Wed Aug 11 01:09:15 2010

Asterisk developer's documentation


loader.c File Reference

Module Loader. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/paths.h"
#include <dirent.h>
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/channel.h"
#include "asterisk/term.h"
#include "asterisk/manager.h"
#include "asterisk/cdr.h"
#include "asterisk/enum.h"
#include "asterisk/rtp.h"
#include "asterisk/http.h"
#include "asterisk/lock.h"
#include "asterisk/features.h"
#include "asterisk/dsp.h"
#include "asterisk/udptl.h"
#include "asterisk/heap.h"
#include <dlfcn.h>
#include "asterisk/md5.h"
#include "asterisk/utils.h"

Include dependency graph for loader.c:

Go to the source code of this file.

Data Structures

struct  ast_module
struct  ast_module_user
struct  load_order
struct  load_order_entry
struct  loadupdate
struct  module_list
struct  module_user_list
struct  reload_classes
struct  reload_queue
struct  reload_queue_item
struct  updaters

Defines

#define RTLD_LOCAL   0
#define RTLD_NOW   0

Functions

struct ast_module_user * __ast_module_user_add (struct ast_module *mod, struct ast_channel *chan)
void __ast_module_user_hangup_all (struct ast_module *mod)
void __ast_module_user_remove (struct ast_module *mod, struct ast_module_user *u)
static struct load_order_entry * add_to_load_order (const char *resource, struct load_order *load_order)
int ast_load_resource (const char *resource_name)
 Load a module.
int ast_loader_register (int(*v)(void))
 Add a procedure to be run when modules have been updated.
int ast_loader_unregister (int(*v)(void))
 Remove a procedure to be run when modules are updated.
int ast_module_check (const char *name)
 Check if module exists.
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.
struct ast_module * ast_module_ref (struct ast_module *mod)
void ast_module_register (const struct ast_module_info *info)
int ast_module_reload (const char *name)
 Reload asterisk modules.
void ast_module_shutdown (void)
 Run the unload() callback for all loaded modules.
void ast_module_unref (struct ast_module *mod)
void ast_module_unregister (const struct ast_module_info *info)
void ast_process_pending_reloads (void)
 Process reload requests received during startup.
int ast_unload_resource (const char *resource_name, enum ast_module_unload_mode force)
 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.
static struct ast_module * find_resource (const char *resource, int do_lock)
static unsigned int inspect_module (const struct ast_module *mod)
static int key_matches (const unsigned char *key1, const unsigned char *key2)
int load_modules (unsigned int preload_only)
static enum ast_module_load_result load_resource (const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap)
static int load_resource_list (struct load_order *load_order, unsigned int global_symbols, int *mod_count)
static int mod_load_cmp (void *a, void *b)
static int printdigest (const unsigned char *d)
static void queue_reload_request (const char *module)
static int resource_name_match (const char *name1_in, const char *name2_in)
static enum ast_module_load_result start_resource (struct ast_module *mod)
static int verify_key (const unsigned char *key)

Variables

static char buildopt_sum [33] = AST_BUILDOPT_SUM
static int do_full_reload = 0
static struct module_list embedded_module_list
static unsigned int embedding = 1
static unsigned char expected_key []
static ast_mutex_t reloadlock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP )
struct ast_module * resource_being_loaded


Detailed Description

Module Loader.

Author:
Mark Spencer <markster@digium.com>

Kevin P. Fleming <kpfleming@digium.com>

Luigi Rizzo <rizzo@icir.org>

  • See ModMngMnt

Definition in file loader.c.


Define Documentation

#define RTLD_LOCAL   0

Definition at line 64 of file loader.c.

#define RTLD_NOW   0

Definition at line 60 of file loader.c.


Function Documentation

struct ast_module_user* __ast_module_user_add ( struct ast_module *  mod,
struct ast_channel *  chan 
) [read]

Definition at line 195 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::usecount, and ast_module::users.

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

00197 {
00198    struct ast_module_user *u = ast_calloc(1, sizeof(*u));
00199 
00200    if (!u)
00201       return NULL;
00202 
00203    u->chan = chan;
00204 
00205    AST_LIST_LOCK(&mod->users);
00206    AST_LIST_INSERT_HEAD(&mod->users, u, entry);
00207    AST_LIST_UNLOCK(&mod->users);
00208 
00209    ast_atomic_fetchadd_int(&mod->usecount, +1);
00210 
00211    ast_update_use_count();
00212 
00213    return u;
00214 }

void __ast_module_user_hangup_all ( struct ast_module *  mod  ) 

void __ast_module_user_remove ( struct ast_module *  mod,
struct ast_module_user *  u 
)

static struct load_order_entry* add_to_load_order ( const char *  resource,
struct load_order *  load_order 
) [static, read]

Definition at line 851 of file loader.c.

References ast_calloc, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_strdup, load_order_entry::resource, and resource_name_match().

Referenced by load_modules().

00852 {
00853    struct load_order_entry *order;
00854 
00855    AST_LIST_TRAVERSE(load_order, order, entry) {
00856       if (!resource_name_match(order->resource, resource))
00857          return NULL;
00858    }
00859 
00860    if (!(order = ast_calloc(1, sizeof(*order))))
00861       return NULL;
00862 
00863    order->resource = ast_strdup(resource);
00864    AST_LIST_INSERT_TAIL(load_order, order, entry);
00865 
00866    return order;
00867 }

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

References AST_LIST_LOCK, AST_LIST_UNLOCK, and load_resource().

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

00835 {
00836    int res;
00837    AST_LIST_LOCK(&module_list);
00838    res = load_resource(resource_name, 0, NULL);
00839    AST_LIST_UNLOCK(&module_list);
00840 
00841    return res;
00842 }

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

References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_malloc, and loadupdate::updater.

Referenced by show_console().

01135 {
01136    struct loadupdate *tmp;
01137 
01138    if (!(tmp = ast_malloc(sizeof(*tmp))))
01139       return -1;
01140 
01141    tmp->updater = v;
01142    AST_LIST_LOCK(&updaters);
01143    AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
01144    AST_LIST_UNLOCK(&updaters);
01145 
01146    return 0;
01147 }

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

01150 {
01151    struct loadupdate *cur;
01152 
01153    AST_LIST_LOCK(&updaters);
01154    AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
01155       if (cur->updater == v)  {
01156          AST_LIST_REMOVE_CURRENT(entry);
01157          break;
01158       }
01159    }
01160    AST_LIST_TRAVERSE_SAFE_END;
01161    AST_LIST_UNLOCK(&updaters);
01162 
01163    return cur ? 0 : -1;
01164 }

int ast_module_check ( const char *  name  ) 

Check if module exists.

Check if module with the name given is loaded.

Definition at line 1121 of file loader.c.

References ast_strlen_zero(), and find_resource().

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

01122 {
01123    struct ast_module *cur;
01124 
01125    if (ast_strlen_zero(name))
01126       return 0;       /* FALSE */
01127 
01128    cur = find_resource(name, 1);
01129 
01130    return (cur != NULL);
01131 }

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

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

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

00546 {
00547    struct ast_module *cur;
00548    int i, which=0, l = strlen(word);
00549    char *ret = NULL;
00550 
00551    if (pos != rpos)
00552       return NULL;
00553 
00554    AST_LIST_LOCK(&module_list);
00555    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00556       if (!strncasecmp(word, cur->resource, l) &&
00557           (cur->info->reload || !needsreload) &&
00558           ++which > state) {
00559          ret = ast_strdup(cur->resource);
00560          break;
00561       }
00562    }
00563    AST_LIST_UNLOCK(&module_list);
00564 
00565    if (!ret) {
00566       for (i=0; !ret && reload_classes[i].name; i++) {
00567          if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
00568             ret = ast_strdup(reload_classes[i].name);
00569       }
00570    }
00571 
00572    return ret;
00573 }

struct ast_module* ast_module_ref ( struct ast_module *  mod  )  [read]

void ast_module_register ( const struct ast_module_info *  info  ) 

Definition at line 133 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::resource, and ast_module::users.

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

int ast_module_reload ( const char *  name  ) 

Reload asterisk modules.

Parameters:
name the name of the module to reload
This function reloads the specified module, or if no modules are specified, it will reload all loaded modules.

Note:
Modules are reloaded using their reload() functions, not unloading them and loading them again.
Returns:
0 if the specified module was not found.
Return values:
1 if the module was found but cannot be reloaded.
-1 if a reload operation is already in progress.
2 if the specfied module was found and reloaded.

Definition at line 640 of file loader.c.

References ast_fully_booted, ast_lastreloadtime, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_mutex_trylock(), ast_mutex_unlock(), ast_tvnow(), ast_verb, ast_verbose, ast_module::declined, ast_module_info::description, ast_module::flags, ast_module::info, LOG_NOTICE, queue_reload_request(), ast_module_info::reload, ast_module::resource, resource_name_match(), and ast_module::running.

Referenced by action_reload(), action_updateconfig(), ast_process_pending_reloads(), handle_reload(), manager_moduleload(), and monitor_sig_flags().

00641 {
00642    struct ast_module *cur;
00643    int res = 0; /* return value. 0 = not found, others, see below */
00644    int i;
00645 
00646    /* If we aren't fully booted, we just pretend we reloaded but we queue this
00647       up to run once we are booted up. */
00648    if (!ast_fully_booted) {
00649       queue_reload_request(name);
00650       return 0;
00651    }
00652 
00653    if (ast_mutex_trylock(&reloadlock)) {
00654       ast_verbose("The previous reload command didn't finish yet\n");
00655       return -1;  /* reload already in progress */
00656    }
00657    ast_lastreloadtime = ast_tvnow();
00658 
00659    /* Call "predefined" reload here first */
00660    for (i = 0; reload_classes[i].name; i++) {
00661       if (!name || !strcasecmp(name, reload_classes[i].name)) {
00662          reload_classes[i].reload_fn();   /* XXX should check error ? */
00663          res = 2; /* found and reloaded */
00664       }
00665    }
00666 
00667    if (name && res) {
00668       ast_mutex_unlock(&reloadlock);
00669       return res;
00670    }
00671 
00672    AST_LIST_LOCK(&module_list);
00673    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00674       const struct ast_module_info *info = cur->info;
00675 
00676       if (name && resource_name_match(name, cur->resource))
00677          continue;
00678 
00679       if (!cur->flags.running || cur->flags.declined) {
00680          if (!name)
00681             continue;
00682          ast_log(LOG_NOTICE, "The module '%s' was not properly initialized.  "
00683             "Before reloading the module, you must run \"module load %s\" "
00684             "and fix whatever is preventing the module from being initialized.\n",
00685             name, name);
00686          res = 2; /* Don't report that the module was not found */
00687          break;
00688       }
00689 
00690       if (!info->reload) { /* cannot be reloaded */
00691          if (res < 1)   /* store result if possible */
00692             res = 1; /* 1 = no reload() method */
00693          continue;
00694       }
00695 
00696       res = 2;
00697       ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
00698       info->reload();
00699    }
00700    AST_LIST_UNLOCK(&module_list);
00701 
00702    ast_mutex_unlock(&reloadlock);
00703 
00704    return res;
00705 }

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 445 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::flags, free, ast_module::info, ast_module::running, ast_module_info::unload, ast_module::usecount, and ast_module::users.

Referenced by quit_handler().

00446 {
00447    struct ast_module *mod;
00448    int somethingchanged = 1, final = 0;
00449 
00450    AST_LIST_LOCK(&module_list);
00451 
00452    /*!\note Some resources, like timers, are started up dynamically, and thus
00453     * may be still in use, even if all channels are dead.  We must therefore
00454     * check the usecount before asking modules to unload. */
00455    do {
00456       if (!somethingchanged) {
00457          /*!\note If we go through the entire list without changing
00458           * anything, ignore the usecounts and unload, then exit. */
00459          final = 1;
00460       }
00461 
00462       /* Reset flag before traversing the list */
00463       somethingchanged = 0;
00464 
00465       AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00466          if (!final && mod->usecount) {
00467             continue;
00468          }
00469          AST_LIST_REMOVE_CURRENT(entry);
00470          if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
00471             mod->info->unload();
00472          }
00473          AST_LIST_HEAD_DESTROY(&mod->users);
00474          free(mod);
00475          somethingchanged = 1;
00476       }
00477       AST_LIST_TRAVERSE_SAFE_END;
00478    } while (somethingchanged && !final);
00479 
00480    AST_LIST_UNLOCK(&module_list);
00481 }

void ast_module_unref ( struct ast_module *  mod  ) 

void ast_module_unregister ( const struct ast_module_info *  info  ) 

Definition at line 171 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::info, and ast_module::users.

00172 {
00173    struct ast_module *mod = NULL;
00174 
00175    /* it is assumed that the users list in the module structure
00176       will already be empty, or we cannot have gotten to this
00177       point
00178    */
00179    AST_LIST_LOCK(&module_list);
00180    AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00181       if (mod->info == info) {
00182          AST_LIST_REMOVE_CURRENT(entry);
00183          break;
00184       }
00185    }
00186    AST_LIST_TRAVERSE_SAFE_END;
00187    AST_LIST_UNLOCK(&module_list);
00188 
00189    if (mod) {
00190       AST_LIST_HEAD_DESTROY(&mod->users);
00191       ast_free(mod);
00192    }
00193 }

void ast_process_pending_reloads ( void   ) 

Process reload requests received during startup.

This function requests that the loader execute the pending reload requests that were queued during server startup.

Note:
This function will do nothing if the server has not completely started up. Once called, the reload queue is emptied, and further invocations will have no affect.

Definition at line 575 of file loader.c.

References ast_free, ast_fully_booted, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_log(), ast_module_reload(), do_full_reload, LOG_NOTICE, and reload_queue_item::module.

Referenced by main().

00576 {
00577    struct reload_queue_item *item;
00578 
00579    if (!ast_fully_booted) {
00580       return;
00581    }
00582 
00583    AST_LIST_LOCK(&reload_queue);
00584 
00585    if (do_full_reload) {
00586       do_full_reload = 0;
00587       AST_LIST_UNLOCK(&reload_queue);
00588       ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
00589       ast_module_reload(NULL);
00590       return;
00591    }
00592 
00593    while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
00594       ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
00595       ast_module_reload(item->module);
00596       ast_free(item);
00597    }
00598 
00599    AST_LIST_UNLOCK(&reload_queue);
00600 }

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 483 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, ast_module_info::unload, and ast_module::usecount.

Referenced by exit_now(), handle_unload(), manager_moduleload(), reload(), and remove_module().

00484 {
00485    struct ast_module *mod;
00486    int res = -1;
00487    int error = 0;
00488 
00489    AST_LIST_LOCK(&module_list);
00490 
00491    if (!(mod = find_resource(resource_name, 0))) {
00492       AST_LIST_UNLOCK(&module_list);
00493       ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
00494       return -1;
00495    }
00496 
00497    if (!mod->flags.running || mod->flags.declined) {
00498       ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
00499       error = 1;
00500    }
00501 
00502    if (!error && (mod->usecount > 0)) {
00503       if (force)
00504          ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
00505             resource_name, mod->usecount);
00506       else {
00507          ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
00508             mod->usecount);
00509          error = 1;
00510       }
00511    }
00512 
00513    if (!error) {
00514       __ast_module_user_hangup_all(mod);
00515       res = mod->info->unload();
00516 
00517       if (res) {
00518          ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
00519          if (force <= AST_FORCE_FIRM)
00520             error = 1;
00521          else
00522             ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
00523       }
00524    }
00525 
00526    if (!error)
00527       mod->flags.running = mod->flags.declined = 0;
00528 
00529    AST_LIST_UNLOCK(&module_list);
00530 
00531    if (!error && !mod->lib && mod->info && mod->info->restore_globals)
00532       mod->info->restore_globals();
00533 
00534 #ifdef LOADABLE_MODULES
00535    if (!error)
00536       unload_dynamic_module(mod);
00537 #endif
00538 
00539    if (!error)
00540       ast_update_use_count();
00541 
00542    return res;
00543 }

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

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

Referenced by ast_var_Modules(), handle_modlist(), and mod_update().

01102 {
01103    struct ast_module *cur;
01104    int unlock = -1;
01105    int total_mod_loaded = 0;
01106 
01107    if (AST_LIST_TRYLOCK(&module_list))
01108       unlock = 0;
01109  
01110    AST_LIST_TRAVERSE(&module_list, cur, entry) {
01111       total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
01112    }
01113 
01114    if (unlock)
01115       AST_LIST_UNLOCK(&module_list);
01116 
01117    return total_mod_loaded;
01118 }

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 1088 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(), handle_request_do(), load_module(), oh323_request(), scheduler_process_request_queue(), sip_request_call(), start_resource(), and unistim_new().

01089 {
01090    /* Notify any module monitors that the use count for a
01091       resource has changed */
01092    struct loadupdate *m;
01093 
01094    AST_LIST_LOCK(&updaters);
01095    AST_LIST_TRAVERSE(&updaters, m, entry)
01096       m->updater();
01097    AST_LIST_UNLOCK(&updaters);
01098 }

static struct ast_module* find_resource ( const char *  resource,
int  do_lock 
) [static, read]

Definition at line 327 of file loader.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_module::resource, and resource_name_match().

Referenced by ast_module_check(), ast_unload_resource(), load_modules(), and load_resource().

00328 {
00329    struct ast_module *cur;
00330 
00331    if (do_lock)
00332       AST_LIST_LOCK(&module_list);
00333 
00334    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00335       if (!resource_name_match(resource, cur->resource))
00336          break;
00337    }
00338 
00339    if (do_lock)
00340       AST_LIST_UNLOCK(&module_list);
00341 
00342    return cur;
00343 }

static unsigned int inspect_module ( const struct ast_module *  mod  )  [static]

Definition at line 707 of file loader.c.

References ast_log(), ast_strlen_zero(), buildopt_sum, ast_module_info::buildopt_sum, ast_module_info::description, ast_module::info, ast_module_info::key, LOG_WARNING, ast_module::resource, and verify_key().

Referenced by load_resource().

00708 {
00709    if (!mod->info->description) {
00710       ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
00711       return 1;
00712    }
00713 
00714    if (!mod->info->key) {
00715       ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
00716       return 1;
00717    }
00718 
00719    if (verify_key((unsigned char *) mod->info->key)) {
00720       ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
00721       return 1;
00722    }
00723 
00724    if (!ast_strlen_zero(mod->info->buildopt_sum) &&
00725        strcmp(buildopt_sum, mod->info->buildopt_sum)) {
00726       ast_log(LOG_WARNING, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
00727       ast_log(LOG_WARNING, "Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
00728       return 1;
00729    }
00730 
00731    return 0;
00732 }

static int key_matches ( const unsigned char *  key1,
const unsigned char *  key2 
) [static]

Definition at line 280 of file loader.c.

Referenced by verify_key().

00281 {
00282    int x;
00283 
00284    for (x = 0; x < 16; x++) {
00285       if (key1[x] != key2[x])
00286          return 0;
00287    }
00288 
00289    return 1;
00290 }

int load_modules ( unsigned  int  ) 

Provided by loader.c

Definition at line 944 of file loader.c.

References add_to_load_order(), ast_config_AST_MODULE_DIR, ast_config_destroy(), ast_config_load2(), ast_free, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), AST_MODULE_CONFIG, ast_opt_quiet, ast_true(), ast_variable_browse(), ast_variable_retrieve(), ast_verb, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, dir, embedding, EVENT_FLAG_SYSTEM, find_resource(), ast_module::flags, ast_module::lib, load_resource_list(), LOG_NOTICE, LOG_WARNING, manager_event, ast_variable::name, ast_variable::next, load_order_entry::resource, ast_module::resource, resource_name_match(), ast_module::running, and ast_variable::value.

Referenced by main().

00945 {
00946    struct ast_config *cfg;
00947    struct ast_module *mod;
00948    struct load_order_entry *order;
00949    struct ast_variable *v;
00950    unsigned int load_count;
00951    struct load_order load_order;
00952    int res = 0;
00953    struct ast_flags config_flags = { 0 };
00954    int modulecount = 0;
00955 
00956 #ifdef LOADABLE_MODULES
00957    struct dirent *dirent;
00958    DIR *dir;
00959 #endif
00960 
00961    /* all embedded modules have registered themselves by now */
00962    embedding = 0;
00963 
00964    ast_verb(1, "Asterisk Dynamic Loader Starting:\n");
00965 
00966    AST_LIST_HEAD_INIT_NOLOCK(&load_order);
00967 
00968    AST_LIST_LOCK(&module_list);
00969 
00970    if (embedded_module_list.first) {
00971       module_list.first = embedded_module_list.first;
00972       module_list.last = embedded_module_list.last;
00973       embedded_module_list.first = NULL;
00974    }
00975 
00976    cfg = ast_config_load2(AST_MODULE_CONFIG, "" /* core, can't reload */, config_flags);
00977    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
00978       ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
00979       goto done;
00980    }
00981 
00982    /* first, find all the modules we have been explicitly requested to load */
00983    for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
00984       if (!strcasecmp(v->name, preload_only ? "preload" : "load")) {
00985          add_to_load_order(v->value, &load_order);
00986       }
00987    }
00988 
00989    /* check if 'autoload' is on */
00990    if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
00991       /* if so, first add all the embedded modules that are not already running to the load order */
00992       AST_LIST_TRAVERSE(&module_list, mod, entry) {
00993          /* if it's not embedded, skip it */
00994          if (mod->lib)
00995             continue;
00996 
00997          if (mod->flags.running)
00998             continue;
00999 
01000          order = add_to_load_order(mod->resource, &load_order);
01001       }
01002 
01003 #ifdef LOADABLE_MODULES
01004       /* if we are allowed to load dynamic modules, scan the directory for
01005          for all available modules and add them as well */
01006       if ((dir  = opendir(ast_config_AST_MODULE_DIR))) {
01007          while ((dirent = readdir(dir))) {
01008             int ld = strlen(dirent->d_name);
01009 
01010             /* Must end in .so to load it.  */
01011 
01012             if (ld < 4)
01013                continue;
01014 
01015             if (strcasecmp(dirent->d_name + ld - 3, ".so"))
01016                continue;
01017 
01018             /* if there is already a module by this name in the module_list,
01019                skip this file */
01020             if (find_resource(dirent->d_name, 0))
01021                continue;
01022 
01023             add_to_load_order(dirent->d_name, &load_order);
01024          }
01025 
01026          closedir(dir);
01027       } else {
01028          if (!ast_opt_quiet)
01029             ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
01030                ast_config_AST_MODULE_DIR);
01031       }
01032 #endif
01033    }
01034 
01035    /* now scan the config for any modules we are prohibited from loading and
01036       remove them from the load order */
01037    for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
01038       if (strcasecmp(v->name, "noload"))
01039          continue;
01040 
01041       AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
01042          if (!resource_name_match(order->resource, v->value)) {
01043             AST_LIST_REMOVE_CURRENT(entry);
01044             ast_free(order->resource);
01045             ast_free(order);
01046          }
01047       }
01048       AST_LIST_TRAVERSE_SAFE_END;
01049    }
01050 
01051    /* we are done with the config now, all the information we need is in the
01052       load_order list */
01053    ast_config_destroy(cfg);
01054 
01055    load_count = 0;
01056    AST_LIST_TRAVERSE(&load_order, order, entry)
01057       load_count++;
01058 
01059    if (load_count)
01060       ast_log(LOG_NOTICE, "%d modules will be loaded.\n", load_count);
01061 
01062    /* first, load only modules that provide global symbols */
01063    if ((res = load_resource_list(&load_order, 1, &modulecount)) < 0) {
01064       goto done;
01065    }
01066 
01067    /* now load everything else */
01068    if ((res = load_resource_list(&load_order, 0, &modulecount)) < 0) {
01069       goto done;
01070    }
01071 
01072 done:
01073    while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
01074       ast_free(order->resource);
01075       ast_free(order);
01076    }
01077 
01078    AST_LIST_UNLOCK(&module_list);
01079    
01080    /* Tell manager clients that are aggressive at logging in that we're done
01081       loading modules. If there's a DNS problem in chan_sip, we might not
01082       even reach this */
01083    manager_event(EVENT_FLAG_SYSTEM, "ModuleLoadReport", "ModuleLoadStatus: Done\r\nModuleSelection: %s\r\nModuleCount: %d\r\n", preload_only ? "Preload" : "All", modulecount);
01084    
01085    return res;
01086 }

static enum ast_module_load_result load_resource ( const char *  resource_name,
unsigned int  global_symbols_only,
struct ast_heap *  resource_heap 
) [static]

loads a resource based upon resource_name. If global_symbols_only is set only modules with global symbols will be loaded.

If the ast_heap is provided (not NULL) the module is found and added to the heap without running the module's load() function. By doing this, modules added to the resource_heap can be initialized later in order by priority.

If the ast_heap is not provided, the module's load function will be executed immediately

Definition at line 780 of file loader.c.

References ast_heap_push(), ast_log(), AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_PRIORITY, AST_MODULE_LOAD_SKIP, AST_MODULE_LOAD_SUCCESS, ast_test_flag, ast_module_info::backup_globals, ast_module::declined, find_resource(), ast_module::flags, ast_module::info, inspect_module(), ast_module::lib, LOG_WARNING, ast_module::running, and start_resource().

Referenced by ast_load_resource(), and load_resource_list().

00781 {
00782    struct ast_module *mod;
00783    enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;
00784 
00785    if ((mod = find_resource(resource_name, 0))) {
00786       if (mod->flags.running) {
00787          ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
00788          return AST_MODULE_LOAD_DECLINE;
00789       }
00790       if (global_symbols_only && !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS))
00791          return AST_MODULE_LOAD_SKIP;
00792    } else {
00793 #ifdef LOADABLE_MODULES
00794       if (!(mod = load_dynamic_module(resource_name, global_symbols_only))) {
00795          /* don't generate a warning message during load_modules() */
00796          if (!global_symbols_only) {
00797             ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00798             return AST_MODULE_LOAD_DECLINE;
00799          } else {
00800             return AST_MODULE_LOAD_SKIP;
00801          }
00802       }
00803 #else
00804       ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00805       return AST_MODULE_LOAD_DECLINE;
00806 #endif
00807    }
00808 
00809    if (inspect_module(mod)) {
00810       ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00811 #ifdef LOADABLE_MODULES
00812       unload_dynamic_module(mod);
00813 #endif
00814       return AST_MODULE_LOAD_DECLINE;
00815    }
00816 
00817    if (!mod->lib && mod->info->backup_globals && mod->info->backup_globals()) {
00818       ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
00819       return AST_MODULE_LOAD_DECLINE;
00820    }
00821 
00822    mod->flags.declined = 0;
00823 
00824    if (resource_heap) {
00825       ast_heap_push(resource_heap, mod);
00826       res = AST_MODULE_LOAD_PRIORITY;
00827    } else {
00828       res = start_resource(mod);
00829    }
00830 
00831    return res;
00832 }

static int load_resource_list ( struct load_order *  load_order,
unsigned int  global_symbols,
int *  mod_count 
) [static]

loads modules in order by load_pri, updates mod_count

Definition at line 886 of file loader.c.

References ast_free, ast_heap_create(), ast_heap_destroy(), ast_heap_pop(), AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_PRIORITY, AST_MODULE_LOAD_SKIP, AST_MODULE_LOAD_SUCCESS, load_resource(), mod_load_cmp(), load_order_entry::resource, and start_resource().

Referenced by load_modules().

00887 {
00888    struct ast_heap *resource_heap;
00889    struct load_order_entry *order;
00890    struct ast_module *mod;
00891    int count = 0;
00892    int res = 0;
00893 
00894    if(!(resource_heap = ast_heap_create(8, mod_load_cmp, -1))) {
00895       return -1;
00896    }
00897 
00898    /* first, add find and add modules to heap */
00899    AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
00900       switch (load_resource(order->resource, global_symbols, resource_heap)) {
00901       case AST_MODULE_LOAD_SUCCESS:
00902       case AST_MODULE_LOAD_DECLINE:
00903          AST_LIST_REMOVE_CURRENT(entry);
00904          ast_free(order->resource);
00905          ast_free(order);
00906          break;
00907       case AST_MODULE_LOAD_FAILURE:
00908          res = -1;
00909          goto done;
00910       case AST_MODULE_LOAD_SKIP:
00911          break;
00912       case AST_MODULE_LOAD_PRIORITY:
00913          AST_LIST_REMOVE_CURRENT(entry);
00914          break;
00915       }
00916    }
00917    AST_LIST_TRAVERSE_SAFE_END;
00918 
00919    /* second remove modules from heap sorted by priority */
00920    while ((mod = ast_heap_pop(resource_heap))) {
00921       switch (start_resource(mod)) {
00922       case AST_MODULE_LOAD_SUCCESS:
00923          count++;
00924       case AST_MODULE_LOAD_DECLINE:
00925          break;
00926       case AST_MODULE_LOAD_FAILURE:
00927          res = -1;
00928          goto done;
00929       case AST_MODULE_LOAD_SKIP:
00930       case AST_MODULE_LOAD_PRIORITY:
00931          break;
00932       }
00933    }
00934 
00935 done:
00936    if (mod_count) {
00937       *mod_count += count;
00938    }
00939    ast_heap_destroy(resource_heap);
00940 
00941    return res;
00942 }

static int mod_load_cmp ( void *  a,
void *  b 
) [static]

Definition at line 869 of file loader.c.

References AST_MODFLAG_LOAD_ORDER, ast_test_flag, ast_module::info, and ast_module_info::load_pri.

Referenced by load_resource_list().

00870 {
00871    struct ast_module *a_mod = (struct ast_module *) a;
00872    struct ast_module *b_mod = (struct ast_module *) b;
00873    int res = -1;
00874    /* if load_pri is not set, default is 255.  Lower is better*/
00875    unsigned char a_pri = ast_test_flag(a_mod->info, AST_MODFLAG_LOAD_ORDER) ? a_mod->info->load_pri : 255;
00876    unsigned char b_pri = ast_test_flag(b_mod->info, AST_MODFLAG_LOAD_ORDER) ? b_mod->info->load_pri : 255;
00877    if (a_pri == b_pri) {
00878       res = 0;
00879    } else if (a_pri < b_pri) {
00880       res = 1;
00881    }
00882    return res;
00883 }

static int printdigest ( const unsigned char *  d  )  [static]

Definition at line 267 of file loader.c.

References ast_debug, and buf.

Referenced by verify_key().

00268 {
00269    int x, pos;
00270    char buf[256]; /* large enough so we don't have to worry */
00271 
00272    for (pos = 0, x = 0; x < 16; x++)
00273       pos += sprintf(buf + pos, " %02x", *d++);
00274 
00275    ast_debug(1, "Unexpected signature:%s\n", buf);
00276 
00277    return 0;
00278 }

static void queue_reload_request ( const char *  module  )  [static]

Definition at line 602 of file loader.c.

References ast_calloc, ast_free, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_strlen_zero(), do_full_reload, LOG_ERROR, and reload_queue_item::module.

Referenced by ast_module_reload().

00603 {
00604    struct reload_queue_item *item;
00605 
00606    AST_LIST_LOCK(&reload_queue);
00607 
00608    if (do_full_reload) {
00609       AST_LIST_UNLOCK(&reload_queue);
00610       return;
00611    }
00612 
00613    if (ast_strlen_zero(module)) {
00614       /* A full reload request (when module is NULL) wipes out any previous
00615          reload requests and causes the queue to ignore future ones */
00616       while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
00617          ast_free(item);
00618       }
00619       do_full_reload = 1;
00620    } else {
00621       /* No reason to add the same module twice */
00622       AST_LIST_TRAVERSE(&reload_queue, item, entry) {
00623          if (!strcasecmp(item->module, module)) {
00624             AST_LIST_UNLOCK(&reload_queue);
00625             return;
00626          }
00627       }
00628       item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
00629       if (!item) {
00630          ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
00631          AST_LIST_UNLOCK(&reload_queue);
00632          return;
00633       }
00634       strcpy(item->module, module);
00635       AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
00636    }
00637    AST_LIST_UNLOCK(&reload_queue);
00638 }

static int resource_name_match ( const char *  name1_in,
const char *  name2_in 
) [static]

Definition at line 309 of file loader.c.

References ast_strdupa.

Referenced by add_to_load_order(), ast_module_reload(), find_resource(), and load_modules().

00310 {
00311    char *name1 = (char *) name1_in;
00312    char *name2 = (char *) name2_in;
00313 
00314    /* trim off any .so extensions */
00315    if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
00316       name1 = ast_strdupa(name1);
00317       name1[strlen(name1) - 3] = '\0';
00318    }
00319    if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
00320       name2 = ast_strdupa(name2);
00321       name2[strlen(name2) - 3] = '\0';
00322    }
00323 
00324    return strcasecmp(name1, name2);
00325 }

static enum ast_module_load_result start_resource ( struct ast_module *  mod  )  [static]

Definition at line 734 of file loader.c.

References ast_fully_booted, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_PRIORITY, AST_MODULE_LOAD_SKIP, AST_MODULE_LOAD_SUCCESS, ast_opt_console, ast_update_use_count(), ast_verb, ast_verbose, COLOR_BLACK, COLOR_BROWN, ast_module::declined, ast_module_info::description, ast_module::flags, ast_module::info, ast_module_info::load, option_verbose, ast_module::resource, ast_module::running, and term_color().

Referenced by load_resource(), and load_resource_list().

00735 {
00736    char tmp[256];
00737    enum ast_module_load_result res;
00738 
00739    if (!mod->info->load) {
00740       return AST_MODULE_LOAD_FAILURE;
00741    }
00742 
00743    res = mod->info->load();
00744 
00745    switch (res) {
00746    case AST_MODULE_LOAD_SUCCESS:
00747       if (!ast_fully_booted) {
00748          ast_verb(1, "%s => (%s)\n", mod->resource, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
00749          if (ast_opt_console && !option_verbose)
00750             ast_verbose( ".");
00751       } else {
00752          ast_verb(1, "Loaded %s => (%s)\n", mod->resource, mod->info->description);
00753       }
00754 
00755       mod->flags.running = 1;
00756 
00757       ast_update_use_count();
00758       break;
00759    case AST_MODULE_LOAD_DECLINE:
00760       mod->flags.declined = 1;
00761       break;
00762    case AST_MODULE_LOAD_FAILURE:
00763    case AST_MODULE_LOAD_SKIP: /* modules should never return this value */
00764    case AST_MODULE_LOAD_PRIORITY:
00765       break;
00766    }
00767 
00768    return res;
00769 }

static int verify_key ( const unsigned char *  key  )  [static]

Definition at line 292 of file loader.c.

References expected_key, key_matches(), MD5Final(), MD5Init(), MD5Update(), and printdigest().

Referenced by inspect_module().

00293 {
00294    struct MD5Context c;
00295    unsigned char digest[16];
00296 
00297    MD5Init(&c);
00298    MD5Update(&c, key, strlen((char *)key));
00299    MD5Final(digest, &c);
00300 
00301    if (key_matches(expected_key, digest))
00302       return 0;
00303 
00304    printdigest(digest);
00305 
00306    return -1;
00307 }


Variable Documentation

char buildopt_sum[33] = AST_BUILDOPT_SUM [static]

Definition at line 78 of file loader.c.

Referenced by inspect_module().

int do_full_reload = 0 [static]

Definition at line 121 of file loader.c.

Referenced by ast_process_pending_reloads(), and queue_reload_request().

Definition at line 105 of file loader.c.

unsigned int embedding = 1 [static]

Definition at line 80 of file loader.c.

Referenced by ast_module_register(), and load_modules().

unsigned char expected_key[] [static]

Initial value:

{ 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
  0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 }

Definition at line 74 of file loader.c.

Referenced by verify_key().

ast_mutex_t reloadlock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ) [static]

Definition at line 114 of file loader.c.

Definition at line 129 of file loader.c.


Generated on Wed Aug 11 01:09:16 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9