Thu Apr 28 2011 17:13:56

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_entryadd_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_moduleast_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_modulefind_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 struct module_list module_list
static struct reload_classes reload_classes []
static struct reload_queue reload_queue
static ast_mutex_t reloadlock = AST_MUTEX_INIT_VALUE
struct ast_moduleresource_being_loaded
static struct updaters updaters

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

{
   struct load_order_entry *order;

   AST_LIST_TRAVERSE(load_order, order, entry) {
      if (!resource_name_match(order->resource, resource))
         return NULL;
   }

   if (!(order = ast_calloc(1, sizeof(*order))))
      return NULL;

   order->resource = ast_strdup(resource);
   AST_LIST_INSERT_TAIL(load_order, order, entry);

   return order;
}
int ast_load_resource ( const char *  resource_name)

Load a module.

Parameters:
resource_nameThe 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().

{
   int res;
   AST_LIST_LOCK(&module_list);
   res = load_resource(resource_name, 0, NULL);
   AST_LIST_UNLOCK(&module_list);

   return res;
}
int ast_loader_register ( int(*)(void)  updater)

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

Parameters:
updaterThe 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:
0on success
-1on 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().

{
   struct loadupdate *tmp;

   if (!(tmp = ast_malloc(sizeof(*tmp))))
      return -1;

   tmp->updater = v;
   AST_LIST_LOCK(&updaters);
   AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
   AST_LIST_UNLOCK(&updaters);

   return 0;
}
int ast_loader_unregister ( int(*)(void)  updater)

Remove a procedure to be run when modules are updated.

Parameters:
updaterThe updater function to unregister.

This removes the given function from the updater list.

Return values:
0on success
-1on 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().

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

{
   struct ast_module *cur;

   if (ast_strlen_zero(name))
      return 0;       /* FALSE */

   cur = find_resource(name, 1);

   return (cur != NULL);
}
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:
lineUnused by this function, but this should be the line we are matching.
wordThe partial name to match.
posThe position the word we are completing is in.
stateThe possible match to return.
rposThe position we should be matching. This should be the same as pos.
needsreloadThis 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:
Apossible completion of the partial match.
NULLif 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, reload_classes::name, ast_module_info::reload, and ast_module::resource.

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

{
   struct ast_module *cur;
   int i, which=0, l = strlen(word);
   char *ret = NULL;

   if (pos != rpos)
      return NULL;

   AST_LIST_LOCK(&module_list);
   AST_LIST_TRAVERSE(&module_list, cur, entry) {
      if (!strncasecmp(word, cur->resource, l) &&
          (cur->info->reload || !needsreload) &&
          ++which > state) {
         ret = ast_strdup(cur->resource);
         break;
      }
   }
   AST_LIST_UNLOCK(&module_list);

   if (!ret) {
      for (i=0; !ret && reload_classes[i].name; i++) {
         if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
            ret = ast_strdup(reload_classes[i].name);
      }
   }

   return ret;
}
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, resource_being_loaded, and ast_module::users.

{
   struct ast_module *mod;

   if (embedding) {
      if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
         return;
      strcpy(mod->resource, info->name);
   } else {
      mod = resource_being_loaded;
   }

   mod->info = info;
   AST_LIST_HEAD_INIT(&mod->users);

   /* during startup, before the loader has been initialized,
      there are no threads, so there is no need to take the lock
      on this list to manipulate it. it is also possible that it
      might be unsafe to use the list lock at that point... so
      let's avoid it altogether
   */
   if (embedding) {
      AST_LIST_INSERT_TAIL(&embedded_module_list, mod, entry);
   } else {
      AST_LIST_LOCK(&module_list);
      /* it is paramount that the new entry be placed at the tail of
         the list, otherwise the code that uses dlopen() to load
         dynamic modules won't be able to find out if the module it
         just opened was registered or failed to load
      */
      AST_LIST_INSERT_TAIL(&module_list, mod, entry);
      AST_LIST_UNLOCK(&module_list);
   }

   /* give the module a copy of its own handle, for later use in registrations and the like */
   *((struct ast_module **) &(info->self)) = mod;
}
int ast_module_reload ( const char *  name)

Reload asterisk modules.

Parameters:
namethe 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:
1if the module was found but cannot be reloaded.
-1if a reload operation is already in progress.
2if 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, reload_classes::name, queue_reload_request(), ast_module_info::reload, reload_classes::reload_fn, 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().

{
   struct ast_module *cur;
   int res = 0; /* return value. 0 = not found, others, see below */
   int i;

   /* If we aren't fully booted, we just pretend we reloaded but we queue this
      up to run once we are booted up. */
   if (!ast_fully_booted) {
      queue_reload_request(name);
      return 0;
   }

   if (ast_mutex_trylock(&reloadlock)) {
      ast_verbose("The previous reload command didn't finish yet\n");
      return -1;  /* reload already in progress */
   }
   ast_lastreloadtime = ast_tvnow();

   /* Call "predefined" reload here first */
   for (i = 0; reload_classes[i].name; i++) {
      if (!name || !strcasecmp(name, reload_classes[i].name)) {
         reload_classes[i].reload_fn();   /* XXX should check error ? */
         res = 2; /* found and reloaded */
      }
   }

   if (name && res) {
      ast_mutex_unlock(&reloadlock);
      return res;
   }

   AST_LIST_LOCK(&module_list);
   AST_LIST_TRAVERSE(&module_list, cur, entry) {
      const struct ast_module_info *info = cur->info;

      if (name && resource_name_match(name, cur->resource))
         continue;

      if (!cur->flags.running || cur->flags.declined) {
         if (!name)
            continue;
         ast_log(LOG_NOTICE, "The module '%s' was not properly initialized.  "
            "Before reloading the module, you must run \"module load %s\" "
            "and fix whatever is preventing the module from being initialized.\n",
            name, name);
         res = 2; /* Don't report that the module was not found */
         break;
      }

      if (!info->reload) { /* cannot be reloaded */
         if (res < 1)   /* store result if possible */
            res = 1; /* 1 = no reload() method */
         continue;
      }

      res = 2;
      ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
      info->reload();
   }
   AST_LIST_UNLOCK(&module_list);

   ast_mutex_unlock(&reloadlock);

   return res;
}
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().

{
   struct ast_module *mod;
   int somethingchanged = 1, final = 0;

   AST_LIST_LOCK(&module_list);

   /*!\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. */
   do {
      if (!somethingchanged) {
         /*!\note If we go through the entire list without changing
          * anything, ignore the usecounts and unload, then exit. */
         final = 1;
      }

      /* Reset flag before traversing the list */
      somethingchanged = 0;

      AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
         if (!final && mod->usecount) {
            continue;
         }
         AST_LIST_REMOVE_CURRENT(entry);
         if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
            mod->info->unload();
         }
         AST_LIST_HEAD_DESTROY(&mod->users);
         free(mod);
         somethingchanged = 1;
      }
      AST_LIST_TRAVERSE_SAFE_END;
   } while (somethingchanged && !final);

   AST_LIST_UNLOCK(&module_list);
}
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.

{
   struct ast_module *mod = NULL;

   /* it is assumed that the users list in the module structure
      will already be empty, or we cannot have gotten to this
      point
   */
   AST_LIST_LOCK(&module_list);
   AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
      if (mod->info == info) {
         AST_LIST_REMOVE_CURRENT(entry);
         break;
      }
   }
   AST_LIST_TRAVERSE_SAFE_END;
   AST_LIST_UNLOCK(&module_list);

   if (mod) {
      AST_LIST_HEAD_DESTROY(&mod->users);
      ast_free(mod);
   }
}
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().

{
   struct reload_queue_item *item;

   if (!ast_fully_booted) {
      return;
   }

   AST_LIST_LOCK(&reload_queue);

   if (do_full_reload) {
      do_full_reload = 0;
      AST_LIST_UNLOCK(&reload_queue);
      ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
      ast_module_reload(NULL);
      return;
   }

   while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
      ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
      ast_module_reload(item->module);
      ast_free(item);
   }

   AST_LIST_UNLOCK(&reload_queue);
}
int ast_unload_resource ( const char *  resource_name,
enum  ast_module_unload_mode 
)

Unload a module.

Parameters:
resource_nameThe name of the module to unload.
ast_module_unload_modeThe 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:
0on success.
-1on 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().

{
   struct ast_module *mod;
   int res = -1;
   int error = 0;

   AST_LIST_LOCK(&module_list);

   if (!(mod = find_resource(resource_name, 0))) {
      AST_LIST_UNLOCK(&module_list);
      ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
      return -1;
   }

   if (!mod->flags.running || mod->flags.declined) {
      ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
      error = 1;
   }

   if (!error && (mod->usecount > 0)) {
      if (force)
         ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
            resource_name, mod->usecount);
      else {
         ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
            mod->usecount);
         error = 1;
      }
   }

   if (!error) {
      __ast_module_user_hangup_all(mod);
      res = mod->info->unload();

      if (res) {
         ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
         if (force <= AST_FORCE_FIRM)
            error = 1;
         else
            ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
      }
   }

   if (!error)
      mod->flags.running = mod->flags.declined = 0;

   AST_LIST_UNLOCK(&module_list);

   if (!error && !mod->lib && mod->info && mod->info->restore_globals)
      mod->info->restore_globals();

#ifdef LOADABLE_MODULES
   if (!error)
      unload_dynamic_module(mod);
#endif

   if (!error)
      ast_update_use_count();

   return res;
}
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:
modentryA callback to an updater function.
likeFor 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().

{
   struct ast_module *cur;
   int unlock = -1;
   int total_mod_loaded = 0;

   if (AST_LIST_TRYLOCK(&module_list))
      unlock = 0;
 
   AST_LIST_TRAVERSE(&module_list, cur, entry) {
      total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
   }

   if (unlock)
      AST_LIST_UNLOCK(&module_list);

   return total_mod_loaded;
}
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().

{
   /* Notify any module monitors that the use count for a
      resource has changed */
   struct loadupdate *m;

   AST_LIST_LOCK(&updaters);
   AST_LIST_TRAVERSE(&updaters, m, entry)
      m->updater();
   AST_LIST_UNLOCK(&updaters);
}
static struct ast_module* find_resource ( const char *  resource,
int  do_lock 
) [static, read]
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().

{
   if (!mod->info->description) {
      ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
      return 1;
   }

   if (!mod->info->key) {
      ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
      return 1;
   }

   if (verify_key((unsigned char *) mod->info->key)) {
      ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
      return 1;
   }

   if (!ast_strlen_zero(mod->info->buildopt_sum) &&
       strcmp(buildopt_sum, mod->info->buildopt_sum)) {
      ast_log(LOG_WARNING, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
      ast_log(LOG_WARNING, "Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
      return 1;
   }

   return 0;
}
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().

{
   int x;

   for (x = 0; x < 16; x++) {
      if (key1[x] != key2[x])
         return 0;
   }

   return 1;
}
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().

{
   struct ast_config *cfg;
   struct ast_module *mod;
   struct load_order_entry *order;
   struct ast_variable *v;
   unsigned int load_count;
   struct load_order load_order;
   int res = 0;
   struct ast_flags config_flags = { 0 };
   int modulecount = 0;

#ifdef LOADABLE_MODULES
   struct dirent *dirent;
   DIR *dir;
#endif

   /* all embedded modules have registered themselves by now */
   embedding = 0;

   ast_verb(1, "Asterisk Dynamic Loader Starting:\n");

   AST_LIST_HEAD_INIT_NOLOCK(&load_order);

   AST_LIST_LOCK(&module_list);

   if (embedded_module_list.first) {
      module_list.first = embedded_module_list.first;
      module_list.last = embedded_module_list.last;
      embedded_module_list.first = NULL;
   }

   cfg = ast_config_load2(AST_MODULE_CONFIG, "" /* core, can't reload */, config_flags);
   if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
      ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
      goto done;
   }

   /* first, find all the modules we have been explicitly requested to load */
   for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
      if (!strcasecmp(v->name, preload_only ? "preload" : "load")) {
         add_to_load_order(v->value, &load_order);
      }
   }

   /* check if 'autoload' is on */
   if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
      /* if so, first add all the embedded modules that are not already running to the load order */
      AST_LIST_TRAVERSE(&module_list, mod, entry) {
         /* if it's not embedded, skip it */
         if (mod->lib)
            continue;

         if (mod->flags.running)
            continue;

         order = add_to_load_order(mod->resource, &load_order);
      }

#ifdef LOADABLE_MODULES
      /* if we are allowed to load dynamic modules, scan the directory for
         for all available modules and add them as well */
      if ((dir  = opendir(ast_config_AST_MODULE_DIR))) {
         while ((dirent = readdir(dir))) {
            int ld = strlen(dirent->d_name);

            /* Must end in .so to load it.  */

            if (ld < 4)
               continue;

            if (strcasecmp(dirent->d_name + ld - 3, ".so"))
               continue;

            /* if there is already a module by this name in the module_list,
               skip this file */
            if (find_resource(dirent->d_name, 0))
               continue;

            add_to_load_order(dirent->d_name, &load_order);
         }

         closedir(dir);
      } else {
         if (!ast_opt_quiet)
            ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
               ast_config_AST_MODULE_DIR);
      }
#endif
   }

   /* now scan the config for any modules we are prohibited from loading and
      remove them from the load order */
   for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
      if (strcasecmp(v->name, "noload"))
         continue;

      AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
         if (!resource_name_match(order->resource, v->value)) {
            AST_LIST_REMOVE_CURRENT(entry);
            ast_free(order->resource);
            ast_free(order);
         }
      }
      AST_LIST_TRAVERSE_SAFE_END;
   }

   /* we are done with the config now, all the information we need is in the
      load_order list */
   ast_config_destroy(cfg);

   load_count = 0;
   AST_LIST_TRAVERSE(&load_order, order, entry)
      load_count++;

   if (load_count)
      ast_log(LOG_NOTICE, "%d modules will be loaded.\n", load_count);

   /* first, load only modules that provide global symbols */
   if ((res = load_resource_list(&load_order, 1, &modulecount)) < 0) {
      goto done;
   }

   /* now load everything else */
   if ((res = load_resource_list(&load_order, 0, &modulecount)) < 0) {
      goto done;
   }

done:
   while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
      ast_free(order->resource);
      ast_free(order);
   }

   AST_LIST_UNLOCK(&module_list);
   
   /* Tell manager clients that are aggressive at logging in that we're done
      loading modules. If there's a DNS problem in chan_sip, we might not
      even reach this */
   manager_event(EVENT_FLAG_SYSTEM, "ModuleLoadReport", "ModuleLoadStatus: Done\r\nModuleSelection: %s\r\nModuleCount: %d\r\n", preload_only ? "Preload" : "All", modulecount);
   
   return res;
}
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().

{
   struct ast_module *mod;
   enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;

   if ((mod = find_resource(resource_name, 0))) {
      if (mod->flags.running) {
         ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
         return AST_MODULE_LOAD_DECLINE;
      }
      if (global_symbols_only && !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS))
         return AST_MODULE_LOAD_SKIP;
   } else {
#ifdef LOADABLE_MODULES
      if (!(mod = load_dynamic_module(resource_name, global_symbols_only))) {
         /* don't generate a warning message during load_modules() */
         if (!global_symbols_only) {
            ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
            return AST_MODULE_LOAD_DECLINE;
         } else {
            return AST_MODULE_LOAD_SKIP;
         }
      }
#else
      ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
      return AST_MODULE_LOAD_DECLINE;
#endif
   }

   if (inspect_module(mod)) {
      ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
#ifdef LOADABLE_MODULES
      unload_dynamic_module(mod);
#endif
      return AST_MODULE_LOAD_DECLINE;
   }

   if (!mod->lib && mod->info->backup_globals && mod->info->backup_globals()) {
      ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
      return AST_MODULE_LOAD_DECLINE;
   }

   mod->flags.declined = 0;

   if (resource_heap) {
      ast_heap_push(resource_heap, mod);
      res = AST_MODULE_LOAD_PRIORITY;
   } else {
      res = start_resource(mod);
   }

   return res;
}
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().

{
   struct ast_heap *resource_heap;
   struct load_order_entry *order;
   struct ast_module *mod;
   int count = 0;
   int res = 0;

   if(!(resource_heap = ast_heap_create(8, mod_load_cmp, -1))) {
      return -1;
   }

   /* first, add find and add modules to heap */
   AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
      switch (load_resource(order->resource, global_symbols, resource_heap)) {
      case AST_MODULE_LOAD_SUCCESS:
      case AST_MODULE_LOAD_DECLINE:
         AST_LIST_REMOVE_CURRENT(entry);
         ast_free(order->resource);
         ast_free(order);
         break;
      case AST_MODULE_LOAD_FAILURE:
         res = -1;
         goto done;
      case AST_MODULE_LOAD_SKIP:
         break;
      case AST_MODULE_LOAD_PRIORITY:
         AST_LIST_REMOVE_CURRENT(entry);
         break;
      }
   }
   AST_LIST_TRAVERSE_SAFE_END;

   /* second remove modules from heap sorted by priority */
   while ((mod = ast_heap_pop(resource_heap))) {
      switch (start_resource(mod)) {
      case AST_MODULE_LOAD_SUCCESS:
         count++;
      case AST_MODULE_LOAD_DECLINE:
         break;
      case AST_MODULE_LOAD_FAILURE:
         res = -1;
         goto done;
      case AST_MODULE_LOAD_SKIP:
      case AST_MODULE_LOAD_PRIORITY:
         break;
      }
   }

done:
   if (mod_count) {
      *mod_count += count;
   }
   ast_heap_destroy(resource_heap);

   return res;
}
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().

{
   struct ast_module *a_mod = (struct ast_module *) a;
   struct ast_module *b_mod = (struct ast_module *) b;
   int res = -1;
   /* if load_pri is not set, default is 255.  Lower is better*/
   unsigned char a_pri = ast_test_flag(a_mod->info, AST_MODFLAG_LOAD_ORDER) ? a_mod->info->load_pri : 255;
   unsigned char b_pri = ast_test_flag(b_mod->info, AST_MODFLAG_LOAD_ORDER) ? b_mod->info->load_pri : 255;
   if (a_pri == b_pri) {
      res = 0;
   } else if (a_pri < b_pri) {
      res = 1;
   }
   return res;
}
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().

{
   int x, pos;
   char buf[256]; /* large enough so we don't have to worry */

   for (pos = 0, x = 0; x < 16; x++)
      pos += sprintf(buf + pos, " %02x", *d++);

   ast_debug(1, "Unexpected signature:%s\n", buf);

   return 0;
}
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().

{
   struct reload_queue_item *item;

   AST_LIST_LOCK(&reload_queue);

   if (do_full_reload) {
      AST_LIST_UNLOCK(&reload_queue);
      return;
   }

   if (ast_strlen_zero(module)) {
      /* A full reload request (when module is NULL) wipes out any previous
         reload requests and causes the queue to ignore future ones */
      while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
         ast_free(item);
      }
      do_full_reload = 1;
   } else {
      /* No reason to add the same module twice */
      AST_LIST_TRAVERSE(&reload_queue, item, entry) {
         if (!strcasecmp(item->module, module)) {
            AST_LIST_UNLOCK(&reload_queue);
            return;
         }
      }
      item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
      if (!item) {
         ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
         AST_LIST_UNLOCK(&reload_queue);
         return;
      }
      strcpy(item->module, module);
      AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
   }
   AST_LIST_UNLOCK(&reload_queue);
}
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().

{
   char *name1 = (char *) name1_in;
   char *name2 = (char *) name2_in;

   /* trim off any .so extensions */
   if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
      name1 = ast_strdupa(name1);
      name1[strlen(name1) - 3] = '\0';
   }
   if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
      name2 = ast_strdupa(name2);
      name2[strlen(name2) - 3] = '\0';
   }

   return strcasecmp(name1, name2);
}
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().

{
   struct MD5Context c;
   unsigned char digest[16];

   MD5Init(&c);
   MD5Update(&c, key, strlen((char *)key));
   MD5Final(digest, &c);

   if (key_matches(expected_key, digest))
      return 0;

   printdigest(digest);

   return -1;
}

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

struct module_list module_list [static]
struct reload_classes reload_classes[] [static]
struct reload_queue reload_queue [static]
ast_mutex_t reloadlock = AST_MUTEX_INIT_VALUE [static]

Definition at line 114 of file loader.c.

Definition at line 129 of file loader.c.

Referenced by ast_module_register().

struct updaters updaters [static]