Mon Mar 19 11:30:50 2012

Asterisk developer's documentation


module.h File Reference

Asterisk module definitions. More...

#include "asterisk/utils.h"

Go to the source code of this file.

Data Structures

struct  ast_module_info

Defines

#define __MODULE_INFO_GLOBALS
#define __MODULE_INFO_SECTION
#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 ast_register_application(app, execute, synopsis, description)   ast_register_application2(app, execute, synopsis, description, ast_module_info->self)
 Register an application.
#define ast_register_application_xml(app, execute)   ast_register_application(app, execute, NULL, NULL)
 Register an application using XML documentation.
#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), AST_MODFLAG_LOAD_ORDER = (1 << 1) }
enum  ast_module_load_priority {
  AST_MODPRI_REALTIME_DEPEND = 10, AST_MODPRI_REALTIME_DEPEND2 = 20, AST_MODPRI_REALTIME_DRIVER = 30, AST_MODPRI_TIMING = 40,
  AST_MODPRI_CHANNEL_DEPEND = 50, AST_MODPRI_CHANNEL_DRIVER = 60, AST_MODPRI_APP_DEPEND = 70, AST_MODPRI_DEVSTATE_PROVIDER = 80,
  AST_MODPRI_DEVSTATE_PLUGIN = 90, AST_MODPRI_CDR_DRIVER = 100, AST_MODPRI_DEFAULT = 128, AST_MODPRI_DEVSTATE_CONSUMER = 150
}
enum  ast_module_load_result {
  AST_MODULE_LOAD_SUCCESS = 0, AST_MODULE_LOAD_DECLINE = 1, AST_MODULE_LOAD_SKIP = 2, AST_MODULE_LOAD_PRIORITY = 3,
  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 *)
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 (const 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.
const char * ast_module_name (const struct ast_module *mod)
 Get the name of a module.
ast_moduleast_module_ref (struct ast_module *)
void ast_module_register (const struct ast_module_info *)
void ast_module_shutdown (void)
 Run the unload() callback for all loaded modules.
void ast_module_unref (struct ast_module *)
void ast_module_unregister (const struct ast_module_info *)
int ast_register_application2 (const char *app, int(*execute)(struct ast_channel *, const char *), const char *synopsis, const char *description, void *mod)
 Register an application.
int ast_unload_resource (const char *resource_name, enum ast_module_unload_mode)
 Unload a module.
int ast_unregister_application (const char *app)
 Unregister an application.
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.

Variables

static struct ast_module_infoast_module_info


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.


Define Documentation

#define __MODULE_INFO_GLOBALS

Definition at line 317 of file module.h.

#define __MODULE_INFO_SECTION

Definition at line 316 of file module.h.

#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 374 of file module.h.

#define AST_MODULE_INFO_STANDARD ( keystr,
desc   ) 

Value:

AST_MODULE_INFO(keystr, AST_MODFLAG_LOAD_ORDER, desc, \
         .load = load_module,       \
         .unload = unload_module,      \
         .load_pri = AST_MODPRI_DEFAULT, \
             )

Definition at line 396 of file module.h.

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

Definition at line 268 of file module.h.

Referenced by canmatch(), dundi_query_read(), dundi_result_read(), dundifunc_read(), exec(), exists(), local_new(), login_exec(), matchmore(), nbs_new(), smdi_msg_read(), and smdi_msg_retrieve_read().

 
#define ast_module_user_hangup_all (  )     __ast_module_user_hangup_all(ast_module_info->self)

Definition at line 270 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 269 of file module.h.

Referenced by canmatch(), dundi_query_read(), dundi_result_read(), dundifunc_read(), exec(), exists(), local_hangup(), login_exec(), matchmore(), nbs_destroy(), smdi_msg_read(), and smdi_msg_retrieve_read().

#define ast_register_application ( app,
execute,
synopsis,
description   )     ast_register_application2(app, execute, synopsis, description, ast_module_info->self)

Register an application.

Parameters:
app Short name of the application
execute a function callback to execute the application. It should return non-zero if the channel needs to be hung up.
synopsis a short description (one line synopsis) of the application
description long description with all of the details about the use of the application
This registers an application with Asterisk's internal application list.
Note:
The individual applications themselves are responsible for registering and unregistering and unregistering their own CLI commands.
Return values:
0 success
-1 failure.

Definition at line 421 of file module.h.

Referenced by load_module().

#define ast_register_application_xml ( app,
execute   )     ast_register_application(app, execute, NULL, NULL)

Register an application using XML documentation.

Parameters:
app Short name of the application
execute a function callback to execute the application. It should return non-zero if the channel needs to be hung up.
This registers an application with Asterisk's internal application list.
Note:
The individual applications themselves are responsible for registering and unregistering and unregistering their own CLI commands.
Return values:
0 success
-1 failure.

Definition at line 437 of file module.h.

Referenced by load_module().

#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.


Enumeration Type Documentation

enum ast_module_flags

Enumerator:
AST_MODFLAG_DEFAULT 
AST_MODFLAG_GLOBAL_SYMBOLS 
AST_MODFLAG_LOAD_ORDER 

Definition at line 198 of file module.h.

00198                       {
00199    AST_MODFLAG_DEFAULT = 0,
00200    AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
00201    AST_MODFLAG_LOAD_ORDER = (1 << 1),
00202 };

enum ast_module_load_priority

Enumerator:
AST_MODPRI_REALTIME_DEPEND  Dependency for a realtime driver
AST_MODPRI_REALTIME_DEPEND2  Second level dependency for a realtime driver (func_curl needs res_curl, but is needed by res_config_curl)
AST_MODPRI_REALTIME_DRIVER  A realtime driver, which provides configuration services for other modules
AST_MODPRI_TIMING  Dependency for a channel (MOH needs timing interfaces to be fully loaded)
AST_MODPRI_CHANNEL_DEPEND  Channel driver dependency (may depend upon realtime, e.g. MOH)
AST_MODPRI_CHANNEL_DRIVER  Channel drivers (provide devicestate)
AST_MODPRI_APP_DEPEND  Dependency for an application
AST_MODPRI_DEVSTATE_PROVIDER  Applications and other modules that _provide_ devicestate (e.g. meetme)
AST_MODPRI_DEVSTATE_PLUGIN  Plugin for a module that provides devstate (e.g. res_calendar_*)
AST_MODPRI_CDR_DRIVER  CDR or CEL backend
AST_MODPRI_DEFAULT  Modules not otherwise defined (such as most apps) will load here
AST_MODPRI_DEVSTATE_CONSUMER  Certain modules, which consume devstate, need to load after all others (e.g. app_queue)

Definition at line 204 of file module.h.

00204                               {
00205    AST_MODPRI_REALTIME_DEPEND =    10,  /*!< Dependency for a realtime driver */
00206    AST_MODPRI_REALTIME_DEPEND2 =   20,  /*!< Second level dependency for a realtime driver (func_curl needs res_curl, but is needed by res_config_curl) */
00207    AST_MODPRI_REALTIME_DRIVER =    30,  /*!< A realtime driver, which provides configuration services for other modules */
00208    AST_MODPRI_TIMING =             40,  /*!< Dependency for a channel (MOH needs timing interfaces to be fully loaded) */
00209    AST_MODPRI_CHANNEL_DEPEND =     50,  /*!< Channel driver dependency (may depend upon realtime, e.g. MOH) */
00210    AST_MODPRI_CHANNEL_DRIVER =     60,  /*!< Channel drivers (provide devicestate) */
00211    AST_MODPRI_APP_DEPEND =         70,  /*!< Dependency for an application */
00212    AST_MODPRI_DEVSTATE_PROVIDER =  80,  /*!< Applications and other modules that _provide_ devicestate (e.g. meetme) */
00213    AST_MODPRI_DEVSTATE_PLUGIN =    90,  /*!< Plugin for a module that provides devstate (e.g. res_calendar_*) */
00214    AST_MODPRI_CDR_DRIVER =        100,  /*!< CDR or CEL backend */
00215    AST_MODPRI_DEFAULT =           128,  /*!< Modules not otherwise defined (such as most apps) will load here */
00216    AST_MODPRI_DEVSTATE_CONSUMER = 150,  /*!< Certain modules, which consume devstate, need to load after all others (e.g. app_queue) */
00217 };

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_PRIORITY  Module is not loaded yet, but is added to prioity heap
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_PRIORITY = 3,   /*!< Module is not loaded yet, but is added to prioity heap */
00065    AST_MODULE_LOAD_FAILURE = -1,   /*!< Module could not be loaded properly */
00066 };

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 204 of file loader.c.

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

Referenced by ast_func_read(), ast_func_read2(), ast_func_write(), and pbx_exec().

00206 {
00207    struct ast_module_user *u = ast_calloc(1, sizeof(*u));
00208 
00209    if (!u)
00210       return NULL;
00211 
00212    u->chan = chan;
00213 
00214    AST_LIST_LOCK(&mod->users);
00215    AST_LIST_INSERT_HEAD(&mod->users, u, entry);
00216    AST_LIST_UNLOCK(&mod->users);
00217 
00218    ast_atomic_fetchadd_int(&mod->usecount, +1);
00219 
00220    ast_update_use_count();
00221 
00222    return u;
00223 }

void __ast_module_user_hangup_all ( struct ast_module  ) 

Definition at line 236 of file loader.c.

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

Referenced by ast_unload_resource().

00237 {
00238    struct ast_module_user *u;
00239 
00240    AST_LIST_LOCK(&mod->users);
00241    while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
00242       ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
00243       ast_atomic_fetchadd_int(&mod->usecount, -1);
00244       ast_free(u);
00245    }
00246    AST_LIST_UNLOCK(&mod->users);
00247 
00248    ast_update_use_count();
00249 }

void __ast_module_user_remove ( struct ast_module ,
struct ast_module_user  
)

Definition at line 225 of file loader.c.

References ast_atomic_fetchadd_int(), ast_free, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_update_use_count(), ast_module_user::entry, ast_module::usecount, and ast_module::users.

Referenced by ast_func_read(), ast_func_read2(), ast_func_write(), and pbx_exec().

00226 {
00227    AST_LIST_LOCK(&mod->users);
00228    AST_LIST_REMOVE(&mod->users, u, entry);
00229    AST_LIST_UNLOCK(&mod->users);
00230    ast_atomic_fetchadd_int(&mod->usecount, -1);
00231    ast_free(u);
00232 
00233    ast_update_use_count();
00234 }

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 initialization 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 885 of file loader.c.

References AST_LIST_LOCK, AST_LIST_UNLOCK, and load_resource().

Referenced by handle_load(), load_module(), and manager_moduleload().

00886 {
00887    int res;
00888    AST_LIST_LOCK(&module_list);
00889    res = load_resource(resource_name, 0, NULL, 0);
00890    AST_LIST_UNLOCK(&module_list);
00891 
00892    return res;
00893 }

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.

Return values:
0 on success
-1 on failure.

Definition at line 1201 of file loader.c.

References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_malloc, and ast_module_user::entry.

01202 {
01203    struct loadupdate *tmp;
01204 
01205    if (!(tmp = ast_malloc(sizeof(*tmp))))
01206       return -1;
01207 
01208    tmp->updater = v;
01209    AST_LIST_LOCK(&updaters);
01210    AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
01211    AST_LIST_UNLOCK(&updaters);
01212 
01213    return 0;
01214 }

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.

Return values:
0 on success
-1 on failure.

Definition at line 1216 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, ast_module_user::entry, and loadupdate::updater.

01217 {
01218    struct loadupdate *cur;
01219 
01220    AST_LIST_LOCK(&updaters);
01221    AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
01222       if (cur->updater == v)  {
01223          AST_LIST_REMOVE_CURRENT(entry);
01224          break;
01225       }
01226    }
01227    AST_LIST_TRAVERSE_SAFE_END;
01228    AST_LIST_UNLOCK(&updaters);
01229 
01230    return cur ? 0 : -1;
01231 }

int ast_module_check ( const char *  name  ) 

Check if module with the name given is loaded.

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

Definition at line 1188 of file loader.c.

References ast_strlen_zero(), and find_resource().

Referenced by ifmodule_read(), load_module(), manager_modulecheck(), and unload_module().

01189 {
01190    struct ast_module *cur;
01191 
01192    if (ast_strlen_zero(name))
01193       return 0;       /* FALSE */
01194 
01195    cur = find_resource(name, 1);
01196 
01197    return (cur != NULL);
01198 }

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.
Return values:
A possible completion of the partial match.
NULL if no matches were found.

Definition at line 574 of file loader.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strdup, ast_module_user::entry, ast_module::info, name, ast_module_info::reload, and ast_module::resource.

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

00575 {
00576    struct ast_module *cur;
00577    int i, which=0, l = strlen(word);
00578    char *ret = NULL;
00579 
00580    if (pos != rpos)
00581       return NULL;
00582 
00583    AST_LIST_LOCK(&module_list);
00584    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00585       if (!strncasecmp(word, cur->resource, l) &&
00586           (cur->info->reload || !needsreload) &&
00587           ++which > state) {
00588          ret = ast_strdup(cur->resource);
00589          break;
00590       }
00591    }
00592    AST_LIST_UNLOCK(&module_list);
00593 
00594    if (!ret) {
00595       for (i=0; !ret && reload_classes[i].name; i++) {
00596          if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
00597             ret = ast_strdup(reload_classes[i].name);
00598       }
00599    }
00600 
00601    return ret;
00602 }

const char* ast_module_name ( const struct ast_module mod  ) 

Get the name of a module.

Parameters:
mod A pointer to the module.
Returns:
the name of the module
Return values:
NULL if mod or mod->info is NULL

Definition at line 99 of file loader.c.

Referenced by acf_retrieve_docs(), and ast_register_application2().

00100 {
00101    if (!mod || !mod->info) {
00102       return NULL;
00103    }
00104 
00105    return mod->info->name;
00106 }

struct ast_module* ast_module_ref ( struct ast_module  ) 

Definition at line 1233 of file loader.c.

References ast_atomic_fetchadd_int(), ast_update_use_count(), and ast_module::usecount.

Referenced by __oh323_new(), agi_handle_command(), alsa_new(), ast_agi_register(), ast_fax_tech_register(), ast_iax2_new(), ast_rtp_instance_new(), ast_srtp_create(), ast_timer_open(), dahdi_new(), dahdi_pri_cc_agent_init(), data_result_generate_node(), fax_session_new(), fax_session_reserve(), find_best_technology(), fn_wrapper(), gtalk_new(), handle_cli_file_convert(), handle_orig(), load_module(), mgcp_new(), moh_alloc(), moh_files_alloc(), mute_add_audiohook(), my_module_ref(), newpvt(), oss_new(), phone_check_exception(), phone_new(), sip_handle_cc(), sip_new(), skinny_new(), and usbradio_new().

01234 {
01235    if (!mod) {
01236       return NULL;
01237    }
01238 
01239    ast_atomic_fetchadd_int(&mod->usecount, +1);
01240    ast_update_use_count();
01241 
01242    return mod;
01243 }

void ast_module_register ( const struct ast_module_info  ) 

Definition at line 142 of file loader.c.

References ast_calloc, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, embedded_module_list, embedding, ast_module_user::entry, ast_module::info, and ast_module::users.

00143 {
00144    struct ast_module *mod;
00145 
00146    if (embedding) {
00147       if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
00148          return;
00149       strcpy(mod->resource, info->name);
00150    } else {
00151       mod = resource_being_loaded;
00152    }
00153 
00154    mod->info = info;
00155    AST_LIST_HEAD_INIT(&mod->users);
00156 
00157    /* during startup, before the loader has been initialized,
00158       there are no threads, so there is no need to take the lock
00159       on this list to manipulate it. it is also possible that it
00160       might be unsafe to use the list lock at that point... so
00161       let's avoid it altogether
00162    */
00163    if (embedding) {
00164       AST_LIST_INSERT_TAIL(&embedded_module_list, mod, entry);
00165    } else {
00166       AST_LIST_LOCK(&module_list);
00167       /* it is paramount that the new entry be placed at the tail of
00168          the list, otherwise the code that uses dlopen() to load
00169          dynamic modules won't be able to find out if the module it
00170          just opened was registered or failed to load
00171       */
00172       AST_LIST_INSERT_TAIL(&module_list, mod, entry);
00173       AST_LIST_UNLOCK(&module_list);
00174    }
00175 
00176    /* give the module a copy of its own handle, for later use in registrations and the like */
00177    *((struct ast_module **) &(info->self)) = mod;
00178 }

void ast_module_shutdown ( void   ) 

Run the unload() callback for all loaded modules.

This function should be called when Asterisk is shutting down gracefully.

Note:
Some resources, like timers, are started up dynamically, and thus may be still in use, even if all channels are dead. We must therefore check the usecount before asking modules to unload.

If we go through the entire list without changing anything, ignore the usecounts and unload, then exit.

Definition at line 474 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, ast_module::declined, ast_module_user::entry, ast_module::flags, free, ast_module::info, ast_module::running, ast_module_info::unload, ast_module::usecount, and ast_module::users.

Referenced by really_quit().

00475 {
00476    struct ast_module *mod;
00477    int somethingchanged = 1, final = 0;
00478 
00479    AST_LIST_LOCK(&module_list);
00480 
00481    /*!\note Some resources, like timers, are started up dynamically, and thus
00482     * may be still in use, even if all channels are dead.  We must therefore
00483     * check the usecount before asking modules to unload. */
00484    do {
00485       if (!somethingchanged) {
00486          /*!\note If we go through the entire list without changing
00487           * anything, ignore the usecounts and unload, then exit. */
00488          final = 1;
00489       }
00490 
00491       /* Reset flag before traversing the list */
00492       somethingchanged = 0;
00493 
00494       AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00495          if (!final && mod->usecount) {
00496             continue;
00497          }
00498          AST_LIST_REMOVE_CURRENT(entry);
00499          if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
00500             mod->info->unload();
00501          }
00502          AST_LIST_HEAD_DESTROY(&mod->users);
00503          free(mod);
00504          somethingchanged = 1;
00505       }
00506       AST_LIST_TRAVERSE_SAFE_END;
00507    } while (somethingchanged && !final);
00508 
00509    AST_LIST_UNLOCK(&module_list);
00510 }

void ast_module_unref ( struct ast_module  ) 

Definition at line 1245 of file loader.c.

References ast_atomic_fetchadd_int(), ast_update_use_count(), and ast_module::usecount.

Referenced by agi_handle_command(), alsa_hangup(), ast_agi_unregister(), ast_bridge_check(), ast_fax_tech_unregister(), ast_rtp_instance_new(), ast_smdi_interface_destroy(), ast_srtp_destroy(), ast_timer_close(), dahdi_destroy_channel_bynum(), dahdi_hangup(), dahdi_pri_cc_agent_destructor(), dahdi_pri_cc_agent_init(), data_result_generate_node(), destroy(), destroy_bridge(), destroy_callback(), destroy_session(), filestream_destructor(), gtalk_hangup(), handle_cli_file_convert(), handle_orig(), iax2_predestroy(), instance_destructor(), local_ast_moh_cleanup(), mgcp_hangup(), my_module_unref(), oh323_hangup(), oss_hangup(), phone_check_exception(), phone_hangup(), sip_cc_monitor_destructor(), sip_hangup(), skinny_hangup(), smart_bridge_operation(), and usbradio_hangup().

01246 {
01247    if (!mod) {
01248       return;
01249    }
01250 
01251    ast_atomic_fetchadd_int(&mod->usecount, -1);
01252    ast_update_use_count();
01253 }

void ast_module_unregister ( const struct ast_module_info  ) 

Definition at line 180 of file loader.c.

References ast_free, AST_LIST_HEAD_DESTROY, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_module_user::entry, ast_module::info, and ast_module::users.

00181 {
00182    struct ast_module *mod = NULL;
00183 
00184    /* it is assumed that the users list in the module structure
00185       will already be empty, or we cannot have gotten to this
00186       point
00187    */
00188    AST_LIST_LOCK(&module_list);
00189    AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00190       if (mod->info == info) {
00191          AST_LIST_REMOVE_CURRENT(entry);
00192          break;
00193       }
00194    }
00195    AST_LIST_TRAVERSE_SAFE_END;
00196    AST_LIST_UNLOCK(&module_list);
00197 
00198    if (mod) {
00199       AST_LIST_HEAD_DESTROY(&mod->users);
00200       ast_free(mod);
00201    }
00202 }

int ast_register_application2 ( const char *  app,
int(*)(struct ast_channel *, const char *)  execute,
const char *  synopsis,
const char *  description,
void *  mod 
)

Register an application.

Parameters:
app Short name of the application
execute a function callback to execute the application. It should return non-zero if the channel needs to be hung up.
synopsis a short description (one line synopsis) of the application
description long description with all of the details about the use of the application
mod module this application belongs to
This registers an application with Asterisk's internal application list.
Note:
The individual applications themselves are responsible for registering and unregistering and unregistering their own CLI commands.
Return values:
0 success
-1 failure.

Definition at line 5803 of file pbx.c.

References ast_app::arguments, ast_calloc, ast_free, ast_log(), ast_module_name(), AST_RWLIST_INSERT_BEFORE_CURRENT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_TRAVERSE, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, AST_STATIC_DOC, ast_string_field_init, ast_string_field_set, ast_strlen_zero(), ast_verb, AST_XML_DOC, ast_xmldoc_build_arguments(), ast_xmldoc_build_description(), ast_xmldoc_build_seealso(), ast_xmldoc_build_synopsis(), ast_xmldoc_build_syntax(), COLOR_BRCYAN, ast_app::docsrc, ast_app::execute, ast_app::list, LOG_WARNING, ast_app::module, ast_app::name, ast_app::seealso, ast_app::syntax, and term_color().

Referenced by ast_cc_init(), ast_features_init(), and load_pbx().

05804 {
05805    struct ast_app *tmp, *cur = NULL;
05806    char tmps[80];
05807    int length, res;
05808 #ifdef AST_XML_DOCS
05809    char *tmpxml;
05810 #endif
05811 
05812    AST_RWLIST_WRLOCK(&apps);
05813    AST_RWLIST_TRAVERSE(&apps, tmp, list) {
05814       if (!(res = strcasecmp(app, tmp->name))) {
05815          ast_log(LOG_WARNING, "Already have an application '%s'\n", app);
05816          AST_RWLIST_UNLOCK(&apps);
05817          return -1;
05818       } else if (res < 0)
05819          break;
05820    }
05821 
05822    length = sizeof(*tmp) + strlen(app) + 1;
05823 
05824    if (!(tmp = ast_calloc(1, length))) {
05825       AST_RWLIST_UNLOCK(&apps);
05826       return -1;
05827    }
05828 
05829    if (ast_string_field_init(tmp, 128)) {
05830       AST_RWLIST_UNLOCK(&apps);
05831       ast_free(tmp);
05832       return -1;
05833    }
05834 
05835    strcpy(tmp->name, app);
05836    tmp->execute = execute;
05837    tmp->module = mod;
05838 
05839 #ifdef AST_XML_DOCS
05840    /* Try to lookup the docs in our XML documentation database */
05841    if (ast_strlen_zero(synopsis) && ast_strlen_zero(description)) {
05842       /* load synopsis */
05843       tmpxml = ast_xmldoc_build_synopsis("application", app, ast_module_name(tmp->module));
05844       ast_string_field_set(tmp, synopsis, tmpxml);
05845       ast_free(tmpxml);
05846 
05847       /* load description */
05848       tmpxml = ast_xmldoc_build_description("application", app, ast_module_name(tmp->module));
05849       ast_string_field_set(tmp, description, tmpxml);
05850       ast_free(tmpxml);
05851 
05852       /* load syntax */
05853       tmpxml = ast_xmldoc_build_syntax("application", app, ast_module_name(tmp->module));
05854       ast_string_field_set(tmp, syntax, tmpxml);
05855       ast_free(tmpxml);
05856 
05857       /* load arguments */
05858       tmpxml = ast_xmldoc_build_arguments("application", app, ast_module_name(tmp->module));
05859       ast_string_field_set(tmp, arguments, tmpxml);
05860       ast_free(tmpxml);
05861 
05862       /* load seealso */
05863       tmpxml = ast_xmldoc_build_seealso("application", app, ast_module_name(tmp->module));
05864       ast_string_field_set(tmp, seealso, tmpxml);
05865       ast_free(tmpxml);
05866       tmp->docsrc = AST_XML_DOC;
05867    } else {
05868 #endif
05869       ast_string_field_set(tmp, synopsis, synopsis);
05870       ast_string_field_set(tmp, description, description);
05871 #ifdef AST_XML_DOCS
05872       tmp->docsrc = AST_STATIC_DOC;
05873    }
05874 #endif
05875 
05876    /* Store in alphabetical order */
05877    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, cur, list) {
05878       if (strcasecmp(tmp->name, cur->name) < 0) {
05879          AST_RWLIST_INSERT_BEFORE_CURRENT(tmp, list);
05880          break;
05881       }
05882    }
05883    AST_RWLIST_TRAVERSE_SAFE_END;
05884    if (!cur)
05885       AST_RWLIST_INSERT_TAIL(&apps, tmp, list);
05886 
05887    ast_verb(2, "Registered application '%s'\n", term_color(tmps, tmp->name, COLOR_BRCYAN, 0, sizeof(tmps)));
05888 
05889    AST_RWLIST_UNLOCK(&apps);
05890 
05891    return 0;
05892 }

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).

Return values:
0 on success.
-1 on error.

Definition at line 512 of file loader.c.

References __ast_module_user_hangup_all(), AST_FORCE_FIRM, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_update_use_count(), ast_module::declined, find_resource(), ast_module::flags, ast_module::info, ast_module::lib, LOG_WARNING, ast_module_info::restore_globals, ast_module::running, unload_dynamic_module(), and ast_module::usecount.

Referenced by handle_unload(), manager_moduleload(), and unload_module().

00513 {
00514    struct ast_module *mod;
00515    int res = -1;
00516    int error = 0;
00517 
00518    AST_LIST_LOCK(&module_list);
00519 
00520    if (!(mod = find_resource(resource_name, 0))) {
00521       AST_LIST_UNLOCK(&module_list);
00522       ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
00523       return -1;
00524    }
00525 
00526    if (!mod->flags.running || mod->flags.declined) {
00527       ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
00528       error = 1;
00529    }
00530 
00531    if (!error && (mod->usecount > 0)) {
00532       if (force)
00533          ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
00534             resource_name, mod->usecount);
00535       else {
00536          ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
00537             mod->usecount);
00538          error = 1;
00539       }
00540    }
00541 
00542    if (!error) {
00543       __ast_module_user_hangup_all(mod);
00544       res = mod->info->unload();
00545 
00546       if (res) {
00547          ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
00548          if (force <= AST_FORCE_FIRM)
00549             error = 1;
00550          else
00551             ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
00552       }
00553    }
00554 
00555    if (!error)
00556       mod->flags.running = mod->flags.declined = 0;
00557 
00558    AST_LIST_UNLOCK(&module_list);
00559 
00560    if (!error && !mod->lib && mod->info && mod->info->restore_globals)
00561       mod->info->restore_globals();
00562 
00563 #ifdef LOADABLE_MODULES
00564    if (!error)
00565       unload_dynamic_module(mod);
00566 #endif
00567 
00568    if (!error)
00569       ast_update_use_count();
00570 
00571    return res;
00572 }

int ast_unregister_application ( const char *  app  ) 

Unregister an application.

Parameters:
app name of the application (does not have to be the same string as the one that was registered)
This unregisters an application from Asterisk's internal application list.

Return values:
0 success
-1 failure

Definition at line 7144 of file pbx.c.

References ast_free, AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_string_field_free_memory, ast_verb, ast_app::list, ast_app::name, and unreference_cached_app().

Referenced by __unload_module(), load_module(), and unload_module().

07145 {
07146    struct ast_app *tmp;
07147 
07148    AST_RWLIST_WRLOCK(&apps);
07149    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, tmp, list) {
07150       if (!strcasecmp(app, tmp->name)) {
07151          unreference_cached_app(tmp);
07152          AST_RWLIST_REMOVE_CURRENT(list);
07153          ast_verb(2, "Unregistered application '%s'\n", tmp->name);
07154          ast_string_field_free_memory(tmp);
07155          ast_free(tmp);
07156          break;
07157       }
07158    }
07159    AST_RWLIST_TRAVERSE_SAFE_END;
07160    AST_RWLIST_UNLOCK(&apps);
07161 
07162    return tmp ? 0 : -1;
07163 }

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 1167 of file loader.c.

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

Referenced by ast_var_Modules(), and handle_modlist().

01169 {
01170    struct ast_module *cur;
01171    int unlock = -1;
01172    int total_mod_loaded = 0;
01173 
01174    if (AST_LIST_TRYLOCK(&module_list))
01175       unlock = 0;
01176  
01177    AST_LIST_TRAVERSE(&module_list, cur, entry) {
01178       total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
01179    }
01180 
01181    if (unlock)
01182       AST_LIST_UNLOCK(&module_list);
01183 
01184    return total_mod_loaded;
01185 }

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 ast_module_user_* functions take care of calling this function for you.

Definition at line 1155 of file loader.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_module_user::entry, 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(), handle_request_do(), oh323_request(), sip_request_call(), start_resource(), and unistim_new().

01156 {
01157    /* Notify any module monitors that the use count for a
01158       resource has changed */
01159    struct loadupdate *m;
01160 
01161    AST_LIST_LOCK(&updaters);
01162    AST_LIST_TRAVERSE(&updaters, m, entry)
01163       m->updater();
01164    AST_LIST_UNLOCK(&updaters);
01165 }


Variable Documentation

struct ast_module_info* ast_module_info [static]

Definition at line 313 of file module.h.


Generated on Mon Mar 19 11:30:50 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7