Mon May 14 04:42:56 2007

Asterisk developer's documentation


loader.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Kevin P. Fleming <kpfleming@digium.com>
00008  * Luigi Rizzo <rizzo@icir.org>
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  *
00023  * \brief Module Loader
00024  * \author Mark Spencer <markster@digium.com>
00025  * \author Kevin P. Fleming <kpfleming@digium.com>
00026  * \author Luigi Rizzo <rizzo@icir.org>
00027  * - See ModMngMnt
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00033 
00034 #include <stdio.h>
00035 #include <dirent.h>
00036 #include <unistd.h>
00037 #include <stdlib.h>
00038 #include <errno.h>
00039 #include <string.h>
00040 
00041 #include "asterisk/linkedlists.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/config.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/term.h"
00048 #include "asterisk/manager.h"
00049 #include "asterisk/cdr.h"
00050 #include "asterisk/enum.h"
00051 #include "asterisk/rtp.h"
00052 #include "asterisk/http.h"
00053 #include "asterisk/lock.h"
00054 
00055 #ifdef DLFCNCOMPAT
00056 #include "asterisk/dlfcn-compat.h"
00057 #else
00058 #include <dlfcn.h>
00059 #endif
00060 
00061 #include "asterisk/md5.h"
00062 #include "asterisk/utils.h"
00063 
00064 #ifndef RTLD_NOW
00065 #define RTLD_NOW 0
00066 #endif
00067 
00068 struct ast_module_user {
00069    struct ast_channel *chan;
00070    AST_LIST_ENTRY(ast_module_user) entry;
00071 };
00072 
00073 AST_LIST_HEAD(module_user_list, ast_module_user);
00074 
00075 static unsigned char expected_key[] =
00076 { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
00077   0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
00078 
00079 static unsigned int embedding = 1; /* we always start out by registering embedded modules,
00080                   since they are here before we dlopen() any
00081                */
00082 
00083 struct ast_module {
00084    const struct ast_module_info *info;
00085    void *lib;              /* the shared lib, or NULL if embedded */
00086    int usecount;              /* the number of 'users' currently in this module */
00087    struct module_user_list users;         /* the list of users in the module */
00088    struct {
00089       unsigned int running:1;
00090       unsigned int declined:1;
00091    } flags;
00092    AST_LIST_ENTRY(ast_module) entry;
00093    char resource[0];
00094 };
00095 
00096 static AST_LIST_HEAD_STATIC(module_list, ast_module);
00097 
00098 struct loadupdate {
00099    int (*updater)(void);
00100    AST_LIST_ENTRY(loadupdate) entry;
00101 };
00102 
00103 static AST_LIST_HEAD_STATIC(updaters, loadupdate);
00104 
00105 AST_MUTEX_DEFINE_STATIC(reloadlock);
00106 
00107 /* when dynamic modules are being loaded, ast_module_register() will
00108    need to know what filename the module was loaded from while it
00109    is being registered
00110 */
00111 struct ast_module *resource_being_loaded;
00112 
00113 /* XXX: should we check for duplicate resource names here? */
00114 
00115 void ast_module_register(const struct ast_module_info *info)
00116 {
00117    struct ast_module *mod;
00118 
00119    if (embedding) {
00120       if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
00121          return;
00122       strcpy(mod->resource, info->name);
00123    } else {
00124       mod = resource_being_loaded;
00125    }
00126 
00127    mod->info = info;
00128    AST_LIST_HEAD_INIT(&mod->users);
00129 
00130    /* during startup, before the loader has been initialized,
00131       there are no threads, so there is no need to take the lock
00132       on this list to manipulate it. it is also possible that it
00133       might be unsafe to use the list lock at that point... so
00134       let's avoid it altogether
00135    */
00136    if (!embedding)
00137       AST_LIST_LOCK(&module_list);
00138 
00139    /* it is paramount that the new entry be placed at the tail of
00140       the list, otherwise the code that uses dlopen() to load
00141       dynamic modules won't be able to find out if the module it
00142       just opened was registered or failed to load
00143    */
00144    AST_LIST_INSERT_TAIL(&module_list, mod, entry);
00145 
00146    if (!embedding)
00147       AST_LIST_UNLOCK(&module_list);
00148 
00149    /* give the module a copy of its own handle, for later use in registrations and the like */
00150    *((struct ast_module **) &(info->self)) = mod;
00151 }
00152 
00153 void ast_module_unregister(const struct ast_module_info *info)
00154 {
00155    struct ast_module *mod = NULL;
00156 
00157    /* it is assumed that the users list in the module structure
00158       will already be empty, or we cannot have gotten to this
00159       point
00160    */
00161    AST_LIST_LOCK(&module_list);
00162    AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00163       if (mod->info == info) {
00164          AST_LIST_REMOVE_CURRENT(&module_list, entry);
00165          break;
00166       }
00167    }
00168    AST_LIST_TRAVERSE_SAFE_END;
00169    AST_LIST_UNLOCK(&module_list);
00170 
00171    if (mod) {
00172       AST_LIST_HEAD_DESTROY(&mod->users);
00173       free(mod);
00174    }
00175 }
00176 
00177 struct ast_module_user *__ast_module_user_add(struct ast_module *mod,
00178                      struct ast_channel *chan)
00179 {
00180    struct ast_module_user *u = ast_calloc(1, sizeof(*u));
00181 
00182    if (!u)
00183       return NULL;
00184 
00185    u->chan = chan;
00186 
00187    AST_LIST_LOCK(&mod->users);
00188    AST_LIST_INSERT_HEAD(&mod->users, u, entry);
00189    AST_LIST_UNLOCK(&mod->users);
00190 
00191    ast_atomic_fetchadd_int(&mod->usecount, +1);
00192 
00193    ast_update_use_count();
00194 
00195    return u;
00196 }
00197 
00198 void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
00199 {
00200    AST_LIST_LOCK(&mod->users);
00201    AST_LIST_REMOVE(&mod->users, u, entry);
00202    AST_LIST_UNLOCK(&mod->users);
00203    ast_atomic_fetchadd_int(&mod->usecount, -1);
00204    free(u);
00205 
00206    ast_update_use_count();
00207 }
00208 
00209 void __ast_module_user_hangup_all(struct ast_module *mod)
00210 {
00211    struct ast_module_user *u;
00212 
00213    AST_LIST_LOCK(&mod->users);
00214    while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
00215       ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
00216       ast_atomic_fetchadd_int(&mod->usecount, -1);
00217       free(u);
00218    }
00219    AST_LIST_UNLOCK(&mod->users);
00220 
00221         ast_update_use_count();
00222 }
00223 
00224 /*! \note
00225  * In addition to modules, the reload command handles some extra keywords
00226  * which are listed here together with the corresponding handlers.
00227  * This table is also used by the command completion code.
00228  */
00229 static struct reload_classes {
00230    const char *name;
00231    int (*reload_fn)(void);
00232 } reload_classes[] = {  /* list in alpha order, longest match first for cli completion */
00233    { "cdr", ast_cdr_engine_reload },
00234    { "dnsmgr", dnsmgr_reload },
00235    { "extconfig", read_config_maps },
00236    { "enum",   ast_enum_reload },
00237    { "manager",   reload_manager },
00238    { "rtp", ast_rtp_reload },
00239    { "http",   ast_http_reload },
00240    { NULL,  NULL }
00241 };
00242 
00243 static int printdigest(const unsigned char *d)
00244 {
00245    int x, pos;
00246    char buf[256]; /* large enough so we don't have to worry */
00247 
00248    for (pos = 0, x = 0; x < 16; x++)
00249       pos += sprintf(buf + pos, " %02x", *d++);
00250 
00251    ast_log(LOG_DEBUG, "Unexpected signature:%s\n", buf);
00252 
00253    return 0;
00254 }
00255 
00256 static int key_matches(const unsigned char *key1, const unsigned char *key2)
00257 {
00258    int x;
00259 
00260    for (x = 0; x < 16; x++) {
00261       if (key1[x] != key2[x])
00262          return 0;
00263    }
00264 
00265    return 1;
00266 }
00267 
00268 static int verify_key(const unsigned char *key)
00269 {
00270    struct MD5Context c;
00271    unsigned char digest[16];
00272 
00273    MD5Init(&c);
00274    MD5Update(&c, key, strlen((char *)key));
00275    MD5Final(digest, &c);
00276 
00277    if (key_matches(expected_key, digest))
00278       return 0;
00279 
00280    printdigest(digest);
00281 
00282    return -1;
00283 }
00284 
00285 static int resource_name_match(const char *name1_in, const char *name2_in)
00286 {
00287    char *name1 = (char *) name1_in;
00288    char *name2 = (char *) name2_in;
00289 
00290    /* trim off any .so extensions */
00291    if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
00292       name1 = ast_strdupa(name1);
00293       name1[strlen(name1) - 3] = '\0';
00294    }
00295    if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
00296       name2 = ast_strdupa(name2);
00297       name2[strlen(name2) - 3] = '\0';
00298    }
00299 
00300    return strcasecmp(name1, name2);
00301 }
00302 
00303 static struct ast_module *find_resource(const char *resource, int do_lock)
00304 {
00305    struct ast_module *cur;
00306 
00307    if (do_lock)
00308       AST_LIST_LOCK(&module_list);
00309 
00310    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00311       if (!resource_name_match(resource, cur->resource))
00312          break;
00313    }
00314 
00315    if (do_lock)
00316       AST_LIST_UNLOCK(&module_list);
00317 
00318    return cur;
00319 }
00320 
00321 #if LOADABLE_MODULES
00322 static void unload_dynamic_module(struct ast_module *mod)
00323 {
00324    void *lib = mod->lib;
00325 
00326    /* WARNING: the structure pointed to by mod is going to
00327       disappear when this operation succeeds, so we can't
00328       dereference it */
00329 
00330    if (lib)
00331       while (!dlclose(lib));
00332 }
00333 
00334 static struct ast_module *load_dynamic_module(const char *resource_in, unsigned int global_symbols_only)
00335 {
00336    char fn[256];
00337    void *lib;
00338    struct ast_module *mod;
00339    char *resource = (char *) resource_in;
00340    unsigned int wants_global;
00341 
00342    if (strcasecmp(resource + strlen(resource) - 3, ".so")) {
00343       resource = alloca(strlen(resource_in) + 3);
00344            strcpy(resource, resource_in);
00345       strcat(resource, ".so");
00346    }
00347 
00348    snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_MODULE_DIR, resource);
00349 
00350    /* make a first load of the module in 'quiet' mode... don't try to resolve
00351       any symbols, and don't export any symbols. this will allow us to peek into
00352       the module's info block (if available) to see what flags it has set */
00353 
00354    if (!(resource_being_loaded = ast_calloc(1, sizeof(*resource_being_loaded) + strlen(resource) + 1)))
00355       return NULL;
00356 
00357    strcpy(resource_being_loaded->resource, resource);
00358 
00359    if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_LOCAL))) {
00360       ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
00361       free(resource_being_loaded);
00362       return NULL;
00363    }
00364 
00365    /* the dlopen() succeeded, let's find out if the module
00366       registered itself */
00367    /* note that this will only work properly as long as
00368       ast_module_register() (which is called by the module's
00369       constructor) places the new module at the tail of the
00370       module_list
00371    */
00372    if (resource_being_loaded != (mod = AST_LIST_LAST(&module_list))) {
00373       ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
00374       /* no, it did not, so close it and return */
00375       while (!dlclose(lib));
00376       /* note that the module's destructor will call ast_module_unregister(),
00377          which will free the structure we allocated in resource_being_loaded */
00378       return NULL;
00379    }
00380 
00381    wants_global = ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS);
00382 
00383    /* if we are being asked only to load modules that provide global symbols,
00384       and this one does not, then close it and return */
00385    if (global_symbols_only && !wants_global) {
00386       while (!dlclose(lib));
00387       return NULL;
00388    }
00389 
00390    /* if the system supports RTLD_NOLOAD, we can just 'promote' the flags
00391       on the already-opened library to what we want... if not, we have to
00392       close it and start over
00393    */
00394 #if defined(HAVE_RTLD_NOLOAD) && !defined(__Darwin__)
00395    if (!dlopen(fn, RTLD_NOLOAD | (wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
00396       ast_log(LOG_WARNING, "Unable to promote flags on module '%s': %s\n", resource_in, dlerror());
00397       while (!dlclose(lib));
00398       free(resource_being_loaded);
00399       return NULL;
00400    }
00401 #else
00402    while (!dlclose(lib));
00403    resource_being_loaded = NULL;
00404 
00405    /* start the load process again */
00406 
00407    if (!(resource_being_loaded = ast_calloc(1, sizeof(*resource_being_loaded) + strlen(resource) + 1)))
00408       return NULL;
00409 
00410    strcpy(resource_being_loaded->resource, resource);
00411 
00412    if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
00413       ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
00414       free(resource_being_loaded);
00415       return NULL;
00416    }
00417 
00418    /* since the module was successfully opened, and it registered itself
00419       the previous time we did that, we're going to assume it worked this
00420       time too :) */
00421 #endif
00422 
00423    AST_LIST_LAST(&module_list)->lib = lib;
00424    resource_being_loaded = NULL;
00425 
00426    return AST_LIST_LAST(&module_list);
00427 }
00428 #endif
00429 
00430 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
00431 {
00432    struct ast_module *mod;
00433    int res = -1;
00434    int error = 0;
00435 
00436    AST_LIST_LOCK(&module_list);
00437 
00438    if (!(mod = find_resource(resource_name, 0))) {
00439       AST_LIST_UNLOCK(&module_list);
00440       return 0;
00441    }
00442 
00443    if (!(mod->flags.running || mod->flags.declined))
00444       error = 1;
00445 
00446    if (!mod->lib) {
00447       ast_log(LOG_WARNING, "Unloading embedded modules is not supported.\n");
00448       error = 1;
00449    }
00450 
00451    if (!error && (mod->usecount > 0)) {
00452       if (force)
00453          ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
00454             resource_name, mod->usecount);
00455       else {
00456          ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
00457             mod->usecount);
00458          error = 1;
00459       }
00460    }
00461 
00462    if (!error) {
00463       __ast_module_user_hangup_all(mod);
00464       res = mod->info->unload();
00465 
00466       if (res) {
00467          ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
00468          if (force <= AST_FORCE_FIRM)
00469             error = 1;
00470          else
00471             ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
00472       }
00473    }
00474 
00475    if (!error)
00476       mod->flags.running = mod->flags.declined = 0;
00477 
00478    AST_LIST_UNLOCK(&module_list);
00479 
00480 #if LOADABLE_MODULES
00481    if (!error)
00482       unload_dynamic_module(mod);
00483 #endif
00484 
00485    if (!error)
00486       ast_update_use_count();
00487 
00488    return res;
00489 }
00490 
00491 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload)
00492 {
00493    struct ast_module *cur;
00494    int i, which=0, l = strlen(word);
00495    char *ret = NULL;
00496 
00497    if (pos != rpos)
00498       return NULL;
00499 
00500    AST_LIST_LOCK(&module_list);
00501    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00502       if (!strncasecmp(word, cur->resource, l) &&
00503           (cur->info->reload || !needsreload) &&
00504           ++which > state) {
00505          ret = strdup(cur->resource);
00506          break;
00507       }
00508    }
00509    AST_LIST_UNLOCK(&module_list);
00510 
00511    if (!ret) {
00512       for (i=0; !ret && reload_classes[i].name; i++) {
00513          if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
00514             ret = strdup(reload_classes[i].name);
00515       }
00516    }
00517 
00518    return ret;
00519 }
00520 
00521 int ast_module_reload(const char *name)
00522 {
00523    struct ast_module *cur;
00524    int res = 0; /* return value. 0 = not found, others, see below */
00525    int i;
00526 
00527    if (ast_mutex_trylock(&reloadlock)) {
00528       ast_verbose("The previous reload command didn't finish yet\n");
00529       return -1;  /* reload already in progress */
00530    }
00531    ast_lastreloadtime = time(NULL);
00532 
00533    /* Call "predefined" reload here first */
00534    for (i = 0; reload_classes[i].name; i++) {
00535       if (!name || !strcasecmp(name, reload_classes[i].name)) {
00536          reload_classes[i].reload_fn();   /* XXX should check error ? */
00537          res = 2; /* found and reloaded */
00538       }
00539    }
00540 
00541    if (name && res) {
00542       ast_mutex_unlock(&reloadlock);
00543       return res;
00544    }
00545 
00546    AST_LIST_LOCK(&module_list);
00547    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00548       const struct ast_module_info *info = cur->info;
00549 
00550       if (name && resource_name_match(name, cur->resource))
00551          continue;
00552 
00553       if (!(cur->flags.running || cur->flags.declined))
00554          continue;
00555 
00556       if (!info->reload) { /* cannot be reloaded */
00557          if (res < 1)   /* store result if possible */
00558             res = 1; /* 1 = no reload() method */
00559          continue;
00560       }
00561 
00562       res = 2;
00563       if (option_verbose > 2)
00564          ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", cur->resource, info->description);
00565       info->reload();
00566    }
00567    AST_LIST_UNLOCK(&module_list);
00568 
00569    ast_mutex_unlock(&reloadlock);
00570 
00571    return res;
00572 }
00573 
00574 static unsigned int inspect_module(const struct ast_module *mod)
00575 {
00576    if (!mod->info->description) {
00577       ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
00578       return 1;
00579    }
00580 
00581    if (!mod->info->key) {
00582       ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
00583       return 1;
00584    }
00585 
00586    if (verify_key((unsigned char *) mod->info->key)) {
00587       ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
00588       return 1;
00589    }
00590 
00591    return 0;
00592 }
00593 
00594 static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only)
00595 {
00596    struct ast_module *mod;
00597    enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;
00598    char tmp[256];
00599 
00600    if ((mod = find_resource(resource_name, 0))) {
00601       if (mod->flags.running) {
00602          ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
00603          return AST_MODULE_LOAD_DECLINE;
00604       }
00605       if (global_symbols_only && !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS))
00606          return AST_MODULE_LOAD_SKIP;
00607    } else {
00608 #if LOADABLE_MODULES
00609       if (!(mod = load_dynamic_module(resource_name, global_symbols_only))) {
00610          /* don't generate a warning message during load_modules() */
00611          if (!global_symbols_only) {
00612             ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00613             return AST_MODULE_LOAD_DECLINE;
00614          } else {
00615             return AST_MODULE_LOAD_SKIP;
00616          }
00617       }
00618 #else
00619       ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00620       return AST_MODULE_LOAD_DECLINE;
00621 #endif
00622    }
00623 
00624    if (inspect_module(mod)) {
00625       ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00626 #if LOADABLE_MODULES
00627       unload_dynamic_module(mod);
00628 #endif
00629       return AST_MODULE_LOAD_DECLINE;
00630    }
00631 
00632    mod->flags.declined = 0;
00633 
00634    if (mod->info->load)
00635       res = mod->info->load();
00636 
00637    switch (res) {
00638    case AST_MODULE_LOAD_SUCCESS:
00639       if (!ast_fully_booted) {
00640          if (option_verbose)
00641             ast_verbose("%s => (%s)\n", resource_name, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
00642          if (ast_opt_console && !option_verbose)
00643             ast_verbose( ".");
00644       } else {
00645          if (option_verbose)
00646             ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", resource_name, mod->info->description);
00647       }
00648 
00649       mod->flags.running = 1;
00650 
00651       ast_update_use_count();
00652       break;
00653    case AST_MODULE_LOAD_DECLINE:
00654       mod->flags.declined = 1;
00655       break;
00656    case AST_MODULE_LOAD_FAILURE:
00657       break;
00658    case AST_MODULE_LOAD_SKIP:
00659       /* modules should never return this value */
00660       break;
00661    }
00662 
00663    return res;
00664 }
00665 
00666 int ast_load_resource(const char *resource_name)
00667 {
00668        AST_LIST_LOCK(&module_list);
00669        load_resource(resource_name, 0);
00670        AST_LIST_UNLOCK(&module_list);
00671 
00672        return 0;
00673 }
00674 
00675 struct load_order_entry {
00676    char *resource;
00677    AST_LIST_ENTRY(load_order_entry) entry;
00678 };
00679 
00680 AST_LIST_HEAD_NOLOCK(load_order, load_order_entry);
00681 
00682 static struct load_order_entry *add_to_load_order(const char *resource, struct load_order *load_order)
00683 {
00684    struct load_order_entry *order;
00685 
00686    AST_LIST_TRAVERSE(load_order, order, entry) {
00687       if (!resource_name_match(order->resource, resource))
00688          return NULL;
00689    }
00690 
00691    if (!(order = ast_calloc(1, sizeof(*order))))
00692       return NULL;
00693 
00694    order->resource = ast_strdup(resource);
00695    AST_LIST_INSERT_TAIL(load_order, order, entry);
00696 
00697    return order;
00698 }
00699 
00700 int load_modules(unsigned int preload_only)
00701 {
00702    struct ast_config *cfg;
00703    struct ast_module *mod;
00704    struct load_order_entry *order;
00705    struct ast_variable *v;
00706    unsigned int load_count;
00707    struct load_order load_order;
00708    int res = 0;
00709 #if LOADABLE_MODULES
00710    struct dirent *dirent;
00711    DIR *dir;
00712 #endif
00713 
00714    /* all embedded modules have registered themselves by now */
00715    embedding = 0;
00716 
00717    if (option_verbose)
00718       ast_verbose("Asterisk Dynamic Loader Starting:\n");
00719 
00720    AST_LIST_HEAD_INIT_NOLOCK(&load_order);
00721 
00722    AST_LIST_LOCK(&module_list);
00723 
00724    if (!(cfg = ast_config_load(AST_MODULE_CONFIG))) {
00725       ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
00726       goto done;
00727    }
00728 
00729    /* first, find all the modules we have been explicitly requested to load */
00730    for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
00731       if (!strcasecmp(v->name, preload_only ? "preload" : "load"))
00732          add_to_load_order(v->value, &load_order);
00733    }
00734 
00735    /* check if 'autoload' is on */
00736    if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
00737       /* if so, first add all the embedded modules that are not already running to the load order */
00738       AST_LIST_TRAVERSE(&module_list, mod, entry) {
00739          /* if it's not embedded, skip it */
00740          if (mod->lib)
00741             continue;
00742 
00743          if (mod->flags.running)
00744             continue;
00745 
00746          order = add_to_load_order(mod->resource, &load_order);
00747       }
00748 
00749 #if LOADABLE_MODULES
00750       /* if we are allowed to load dynamic modules, scan the directory for
00751          for all available modules and add them as well */
00752       if ((dir  = opendir(ast_config_AST_MODULE_DIR))) {
00753          while ((dirent = readdir(dir))) {
00754             int ld = strlen(dirent->d_name);
00755 
00756             /* Must end in .so to load it.  */
00757 
00758             if (ld < 4)
00759                continue;
00760 
00761             if (strcasecmp(dirent->d_name + ld - 3, ".so"))
00762                continue;
00763 
00764             /* if there is already a module by this name in the module_list,
00765                skip this file */
00766             if (find_resource(dirent->d_name, 0))
00767                continue;
00768 
00769             add_to_load_order(dirent->d_name, &load_order);
00770          }
00771 
00772          closedir(dir);
00773       } else {
00774          if (!ast_opt_quiet)
00775             ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
00776                ast_config_AST_MODULE_DIR);
00777       }
00778 #endif
00779    }
00780 
00781    /* now scan the config for any modules we are prohibited from loading and
00782       remove them from the load order */
00783    for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
00784       if (strcasecmp(v->name, "noload"))
00785          continue;
00786 
00787       AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
00788          if (!resource_name_match(order->resource, v->value)) {
00789             AST_LIST_REMOVE_CURRENT(&load_order, entry);
00790             free(order->resource);
00791             free(order);
00792          }
00793       }
00794       AST_LIST_TRAVERSE_SAFE_END;
00795    }
00796 
00797    /* we are done with the config now, all the information we need is in the
00798       load_order list */
00799    ast_config_destroy(cfg);
00800 
00801    load_count = 0;
00802    AST_LIST_TRAVERSE(&load_order, order, entry)
00803       load_count++;
00804 
00805    if (load_count)
00806       ast_log(LOG_NOTICE, "%d modules will be loaded.\n", load_count);
00807 
00808    /* first, load only modules that provide global symbols */
00809    AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
00810       switch (load_resource(order->resource, 1)) {
00811       case AST_MODULE_LOAD_SUCCESS:
00812       case AST_MODULE_LOAD_DECLINE:
00813          AST_LIST_REMOVE_CURRENT(&load_order, entry);
00814          free(order->resource);
00815          free(order);
00816          break;
00817       case AST_MODULE_LOAD_FAILURE:
00818          res = -1;
00819          goto done;
00820       case AST_MODULE_LOAD_SKIP:
00821          /* try again later */
00822          break;
00823       }
00824    }
00825    AST_LIST_TRAVERSE_SAFE_END;
00826 
00827    /* now load everything else */
00828    AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
00829       switch (load_resource(order->resource, 0)) {
00830       case AST_MODULE_LOAD_SUCCESS:
00831       case AST_MODULE_LOAD_DECLINE:
00832          AST_LIST_REMOVE_CURRENT(&load_order, entry);
00833          free(order->resource);
00834          free(order);
00835          break;
00836       case AST_MODULE_LOAD_FAILURE:
00837          res = -1;
00838          goto done;
00839       case AST_MODULE_LOAD_SKIP:
00840          /* should not happen */
00841          break;
00842       }
00843    }
00844    AST_LIST_TRAVERSE_SAFE_END;
00845 
00846 done:
00847    while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
00848       free(order->resource);
00849       free(order);
00850    }
00851 
00852    AST_LIST_UNLOCK(&module_list);
00853 
00854    return res;
00855 }
00856 
00857 void ast_update_use_count(void)
00858 {
00859    /* Notify any module monitors that the use count for a
00860       resource has changed */
00861    struct loadupdate *m;
00862 
00863    AST_LIST_LOCK(&module_list);
00864    AST_LIST_TRAVERSE(&updaters, m, entry)
00865       m->updater();
00866    AST_LIST_UNLOCK(&module_list);
00867 }
00868 
00869 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
00870             const char *like)
00871 {
00872    struct ast_module *cur;
00873    int unlock = -1;
00874    int total_mod_loaded = 0;
00875 
00876    if (AST_LIST_TRYLOCK(&module_list))
00877       unlock = 0;
00878 
00879    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00880       total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
00881    }
00882 
00883    if (unlock)
00884       AST_LIST_UNLOCK(&module_list);
00885 
00886    return total_mod_loaded;
00887 }
00888 
00889 int ast_module_check(char *name)
00890 {
00891    struct ast_module *cur;
00892    int unlock = -1;
00893    int res = 0;
00894 
00895    if (ast_strlen_zero(name))
00896       return 0;   /* FALSE */
00897 
00898    if (ast_mutex_trylock(&module_list.lock))
00899       unlock = 0;
00900    AST_LIST_TRAVERSE(&module_list, cur, entry)
00901       if (!res && !strcasecmp(name, cur->resource))
00902          res = 1;
00903    if (unlock)
00904       AST_LIST_UNLOCK(&module_list);
00905    return res;
00906 }
00907 
00908 int ast_loader_register(int (*v)(void))
00909 {
00910    struct loadupdate *tmp;
00911 
00912    if (!(tmp = ast_malloc(sizeof(*tmp))))
00913       return -1;
00914 
00915    tmp->updater = v;
00916    AST_LIST_LOCK(&module_list);
00917    AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
00918    AST_LIST_UNLOCK(&module_list);
00919 
00920    return 0;
00921 }
00922 
00923 int ast_loader_unregister(int (*v)(void))
00924 {
00925    struct loadupdate *cur;
00926 
00927    AST_LIST_LOCK(&module_list);
00928    AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
00929       if (cur->updater == v)  {
00930          AST_LIST_REMOVE_CURRENT(&updaters, entry);
00931          break;
00932       }
00933    }
00934    AST_LIST_TRAVERSE_SAFE_END;
00935    AST_LIST_UNLOCK(&module_list);
00936 
00937    return cur ? 0 : -1;
00938 }
00939 
00940 struct ast_module *ast_module_ref(struct ast_module *mod)
00941 {
00942    ast_atomic_fetchadd_int(&mod->usecount, +1);
00943    ast_update_use_count();
00944 
00945    return mod;
00946 }
00947 
00948 void ast_module_unref(struct ast_module *mod)
00949 {
00950    ast_atomic_fetchadd_int(&mod->usecount, -1);
00951    ast_update_use_count();
00952 }

Generated on Mon May 14 04:42:56 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1