00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "asterisk.h"
00035
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 375862 $")
00037
00038 #include "asterisk/_private.h"
00039 #include "asterisk/paths.h"
00040 #include <dirent.h>
00041
00042 #include "asterisk/linkedlists.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/config.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/term.h"
00047 #include "asterisk/manager.h"
00048 #include "asterisk/cdr.h"
00049 #include "asterisk/enum.h"
00050 #include "asterisk/http.h"
00051 #include "asterisk/lock.h"
00052 #include "asterisk/features.h"
00053 #include "asterisk/dsp.h"
00054 #include "asterisk/udptl.h"
00055 #include "asterisk/heap.h"
00056 #include "asterisk/app.h"
00057 #include "asterisk/test.h"
00058
00059 #include <dlfcn.h>
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 #ifndef RTLD_LOCAL
00069 #define RTLD_LOCAL 0
00070 #endif
00071
00072 struct ast_module_user {
00073 struct ast_channel *chan;
00074 AST_LIST_ENTRY(ast_module_user) entry;
00075 };
00076
00077 AST_LIST_HEAD(module_user_list, ast_module_user);
00078
00079 static const unsigned char expected_key[] =
00080 { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
00081 0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
00082
00083 static char buildopt_sum[33] = AST_BUILDOPT_SUM;
00084
00085 static unsigned int embedding = 1;
00086
00087
00088
00089 struct ast_module {
00090 const struct ast_module_info *info;
00091 void *lib;
00092 int usecount;
00093 struct module_user_list users;
00094 struct {
00095 unsigned int running:1;
00096 unsigned int declined:1;
00097 } flags;
00098 AST_LIST_ENTRY(ast_module) entry;
00099 char resource[0];
00100 };
00101
00102 static AST_LIST_HEAD_STATIC(module_list, ast_module);
00103
00104 const char *ast_module_name(const struct ast_module *mod)
00105 {
00106 if (!mod || !mod->info) {
00107 return NULL;
00108 }
00109
00110 return mod->info->name;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 static struct module_list embedded_module_list;
00120
00121 struct loadupdate {
00122 int (*updater)(void);
00123 AST_LIST_ENTRY(loadupdate) entry;
00124 };
00125
00126 static AST_LIST_HEAD_STATIC(updaters, loadupdate);
00127
00128 AST_MUTEX_DEFINE_STATIC(reloadlock);
00129
00130 struct reload_queue_item {
00131 AST_LIST_ENTRY(reload_queue_item) entry;
00132 char module[0];
00133 };
00134
00135 static int do_full_reload = 0;
00136
00137 static AST_LIST_HEAD_STATIC(reload_queue, reload_queue_item);
00138
00139
00140
00141
00142
00143 static struct ast_module *resource_being_loaded;
00144
00145
00146
00147 void ast_module_register(const struct ast_module_info *info)
00148 {
00149 struct ast_module *mod;
00150
00151 if (embedding) {
00152 if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
00153 return;
00154 strcpy(mod->resource, info->name);
00155 } else {
00156 mod = resource_being_loaded;
00157 }
00158
00159 mod->info = info;
00160 AST_LIST_HEAD_INIT(&mod->users);
00161
00162
00163
00164
00165
00166
00167
00168 if (embedding) {
00169 AST_LIST_INSERT_TAIL(&embedded_module_list, mod, entry);
00170 } else {
00171 AST_LIST_LOCK(&module_list);
00172
00173
00174
00175
00176
00177 AST_LIST_INSERT_TAIL(&module_list, mod, entry);
00178 AST_LIST_UNLOCK(&module_list);
00179 }
00180
00181
00182 *((struct ast_module **) &(info->self)) = mod;
00183 }
00184
00185 void ast_module_unregister(const struct ast_module_info *info)
00186 {
00187 struct ast_module *mod = NULL;
00188
00189
00190
00191
00192
00193 AST_LIST_LOCK(&module_list);
00194 AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00195 if (mod->info == info) {
00196 AST_LIST_REMOVE_CURRENT(entry);
00197 break;
00198 }
00199 }
00200 AST_LIST_TRAVERSE_SAFE_END;
00201 AST_LIST_UNLOCK(&module_list);
00202
00203 if (mod) {
00204 AST_LIST_HEAD_DESTROY(&mod->users);
00205 ast_free(mod);
00206 }
00207 }
00208
00209 struct ast_module_user *__ast_module_user_add(struct ast_module *mod, struct ast_channel *chan)
00210 {
00211 struct ast_module_user *u;
00212
00213 u = ast_calloc(1, sizeof(*u));
00214 if (!u) {
00215 return NULL;
00216 }
00217
00218 u->chan = chan;
00219
00220 AST_LIST_LOCK(&mod->users);
00221 AST_LIST_INSERT_HEAD(&mod->users, u, entry);
00222 AST_LIST_UNLOCK(&mod->users);
00223
00224 ast_atomic_fetchadd_int(&mod->usecount, +1);
00225
00226 ast_update_use_count();
00227
00228 return u;
00229 }
00230
00231 void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
00232 {
00233 if (!u) {
00234 return;
00235 }
00236 AST_LIST_LOCK(&mod->users);
00237 AST_LIST_REMOVE(&mod->users, u, entry);
00238 AST_LIST_UNLOCK(&mod->users);
00239 ast_atomic_fetchadd_int(&mod->usecount, -1);
00240 ast_free(u);
00241
00242 ast_update_use_count();
00243 }
00244
00245 void __ast_module_user_hangup_all(struct ast_module *mod)
00246 {
00247 struct ast_module_user *u;
00248
00249 AST_LIST_LOCK(&mod->users);
00250 while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
00251 if (u->chan) {
00252 ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
00253 }
00254 ast_atomic_fetchadd_int(&mod->usecount, -1);
00255 ast_free(u);
00256 }
00257 AST_LIST_UNLOCK(&mod->users);
00258
00259 ast_update_use_count();
00260 }
00261
00262
00263
00264
00265
00266
00267 static struct reload_classes {
00268 const char *name;
00269 int (*reload_fn)(void);
00270 } reload_classes[] = {
00271 { "cdr", ast_cdr_engine_reload },
00272 { "dnsmgr", dnsmgr_reload },
00273 { "extconfig", read_config_maps },
00274 { "enum", ast_enum_reload },
00275 { "manager", reload_manager },
00276 { "http", ast_http_reload },
00277 { "logger", logger_reload },
00278 { "features", ast_features_reload },
00279 { "dsp", ast_dsp_reload},
00280 { "udptl", ast_udptl_reload },
00281 { "indications", ast_indications_reload },
00282 { "cel", ast_cel_engine_reload },
00283 { "plc", ast_plc_reload },
00284 { NULL, NULL }
00285 };
00286
00287 static int printdigest(const unsigned char *d)
00288 {
00289 int x, pos;
00290 char buf[256];
00291
00292 for (pos = 0, x = 0; x < 16; x++)
00293 pos += sprintf(buf + pos, " %02x", *d++);
00294
00295 ast_debug(1, "Unexpected signature:%s\n", buf);
00296
00297 return 0;
00298 }
00299
00300 static int key_matches(const unsigned char *key1, const unsigned char *key2)
00301 {
00302 int x;
00303
00304 for (x = 0; x < 16; x++) {
00305 if (key1[x] != key2[x])
00306 return 0;
00307 }
00308
00309 return 1;
00310 }
00311
00312 static int verify_key(const unsigned char *key)
00313 {
00314 struct MD5Context c;
00315 unsigned char digest[16];
00316
00317 MD5Init(&c);
00318 MD5Update(&c, key, strlen((char *)key));
00319 MD5Final(digest, &c);
00320
00321 if (key_matches(expected_key, digest))
00322 return 0;
00323
00324 printdigest(digest);
00325
00326 return -1;
00327 }
00328
00329 static int resource_name_match(const char *name1_in, const char *name2_in)
00330 {
00331 char *name1 = (char *) name1_in;
00332 char *name2 = (char *) name2_in;
00333
00334
00335 if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
00336 name1 = ast_strdupa(name1);
00337 name1[strlen(name1) - 3] = '\0';
00338 }
00339 if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
00340 name2 = ast_strdupa(name2);
00341 name2[strlen(name2) - 3] = '\0';
00342 }
00343
00344 return strcasecmp(name1, name2);
00345 }
00346
00347 static struct ast_module *find_resource(const char *resource, int do_lock)
00348 {
00349 struct ast_module *cur;
00350
00351 if (do_lock)
00352 AST_LIST_LOCK(&module_list);
00353
00354 AST_LIST_TRAVERSE(&module_list, cur, entry) {
00355 if (!resource_name_match(resource, cur->resource))
00356 break;
00357 }
00358
00359 if (do_lock)
00360 AST_LIST_UNLOCK(&module_list);
00361
00362 return cur;
00363 }
00364
00365 #ifdef LOADABLE_MODULES
00366 static void unload_dynamic_module(struct ast_module *mod)
00367 {
00368 void *lib = mod->lib;
00369
00370
00371
00372
00373
00374 if (lib)
00375 while (!dlclose(lib));
00376 }
00377
00378 static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required);
00379
00380 static struct ast_module *load_dynamic_module(const char *resource_in, unsigned int global_symbols_only, struct ast_heap *resource_heap)
00381 {
00382 char fn[PATH_MAX] = "";
00383 void *lib = NULL;
00384 struct ast_module *mod;
00385 unsigned int wants_global;
00386 int space;
00387 int missing_so = 0;
00388
00389 space = sizeof(*resource_being_loaded) + strlen(resource_in) + 1;
00390 if (strcasecmp(resource_in + strlen(resource_in) - 3, ".so")) {
00391 missing_so = 1;
00392 space += 3;
00393 }
00394
00395 snprintf(fn, sizeof(fn), "%s/%s%s", ast_config_AST_MODULE_DIR, resource_in, missing_so ? ".so" : "");
00396
00397
00398
00399
00400
00401 resource_being_loaded = ast_calloc(1, space);
00402 if (!resource_being_loaded)
00403 return NULL;
00404 strcpy(resource_being_loaded->resource, resource_in);
00405 if (missing_so)
00406 strcat(resource_being_loaded->resource, ".so");
00407
00408 if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_LOCAL))) {
00409 ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
00410 ast_free(resource_being_loaded);
00411 return NULL;
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421 if (resource_being_loaded != (mod = AST_LIST_LAST(&module_list))) {
00422 ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
00423
00424 while (!dlclose(lib));
00425
00426
00427 return NULL;
00428 }
00429
00430 wants_global = ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS);
00431
00432
00433
00434 if (global_symbols_only && !wants_global) {
00435 while (!dlclose(lib));
00436 return NULL;
00437 }
00438
00439
00440
00441
00442
00443
00444 #if !defined(HAVE_ATTRIBUTE_weak_import) && !defined(HAVE_ATTRIBUTE_weakref)
00445 if (!ast_strlen_zero(mod->info->nonoptreq)) {
00446
00447 char *each, *required_resource = ast_strdupa(mod->info->nonoptreq);
00448 while ((each = strsep(&required_resource, ","))) {
00449 struct ast_module *dependency;
00450 each = ast_strip(each);
00451 dependency = find_resource(each, 0);
00452
00453 if (!dependency) {
00454 load_resource(each, global_symbols_only, resource_heap, 1);
00455 }
00456 }
00457 }
00458 #endif
00459
00460 while (!dlclose(lib));
00461 resource_being_loaded = NULL;
00462
00463
00464 resource_being_loaded = ast_calloc(1, space);
00465 if (!resource_being_loaded)
00466 return NULL;
00467 strcpy(resource_being_loaded->resource, resource_in);
00468 if (missing_so)
00469 strcat(resource_being_loaded->resource, ".so");
00470
00471 if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
00472 ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
00473 ast_free(resource_being_loaded);
00474 return NULL;
00475 }
00476
00477
00478
00479
00480
00481 AST_LIST_LAST(&module_list)->lib = lib;
00482 resource_being_loaded = NULL;
00483
00484 return AST_LIST_LAST(&module_list);
00485 }
00486 #endif
00487
00488 void ast_module_shutdown(void)
00489 {
00490 struct ast_module *mod;
00491 int somethingchanged = 1, final = 0;
00492
00493 AST_LIST_LOCK(&module_list);
00494
00495
00496
00497
00498 do {
00499 if (!somethingchanged) {
00500
00501
00502 final = 1;
00503 }
00504
00505
00506 somethingchanged = 0;
00507
00508 AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00509 if (!final && mod->usecount) {
00510 continue;
00511 }
00512 AST_LIST_REMOVE_CURRENT(entry);
00513 if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
00514 mod->info->unload();
00515 }
00516 AST_LIST_HEAD_DESTROY(&mod->users);
00517 free(mod);
00518 somethingchanged = 1;
00519 }
00520 AST_LIST_TRAVERSE_SAFE_END;
00521 } while (somethingchanged && !final);
00522
00523 AST_LIST_UNLOCK(&module_list);
00524 }
00525
00526 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
00527 {
00528 struct ast_module *mod;
00529 int res = -1;
00530 int error = 0;
00531
00532 AST_LIST_LOCK(&module_list);
00533
00534 if (!(mod = find_resource(resource_name, 0))) {
00535 AST_LIST_UNLOCK(&module_list);
00536 ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
00537 return -1;
00538 }
00539
00540 if (!mod->flags.running || mod->flags.declined) {
00541 ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
00542 error = 1;
00543 }
00544
00545 if (!error && (mod->usecount > 0)) {
00546 if (force)
00547 ast_log(LOG_WARNING, "Warning: Forcing removal of module '%s' with use count %d\n",
00548 resource_name, mod->usecount);
00549 else {
00550 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
00551 mod->usecount);
00552 error = 1;
00553 }
00554 }
00555
00556 if (!error) {
00557 __ast_module_user_hangup_all(mod);
00558 res = mod->info->unload();
00559
00560 if (res) {
00561 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
00562 if (force <= AST_FORCE_FIRM)
00563 error = 1;
00564 else
00565 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
00566 }
00567 }
00568
00569 if (!error)
00570 mod->flags.running = mod->flags.declined = 0;
00571
00572 AST_LIST_UNLOCK(&module_list);
00573
00574 if (!error && !mod->lib && mod->info && mod->info->restore_globals)
00575 mod->info->restore_globals();
00576
00577 #ifdef LOADABLE_MODULES
00578 if (!error) {
00579 unload_dynamic_module(mod);
00580 ast_test_suite_event_notify("MODULE_UNLOAD", "Message: %s", resource_name);
00581 }
00582 #endif
00583
00584 if (!error)
00585 ast_update_use_count();
00586
00587 return res;
00588 }
00589
00590 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload)
00591 {
00592 struct ast_module *cur;
00593 int i, which=0, l = strlen(word);
00594 char *ret = NULL;
00595
00596 if (pos != rpos)
00597 return NULL;
00598
00599 AST_LIST_LOCK(&module_list);
00600 AST_LIST_TRAVERSE(&module_list, cur, entry) {
00601 if (!strncasecmp(word, cur->resource, l) &&
00602 (cur->info->reload || !needsreload) &&
00603 ++which > state) {
00604 ret = ast_strdup(cur->resource);
00605 break;
00606 }
00607 }
00608 AST_LIST_UNLOCK(&module_list);
00609
00610 if (!ret) {
00611 for (i=0; !ret && reload_classes[i].name; i++) {
00612 if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
00613 ret = ast_strdup(reload_classes[i].name);
00614 }
00615 }
00616
00617 return ret;
00618 }
00619
00620 void ast_process_pending_reloads(void)
00621 {
00622 struct reload_queue_item *item;
00623
00624 if (!ast_fully_booted) {
00625 return;
00626 }
00627
00628 AST_LIST_LOCK(&reload_queue);
00629
00630 if (do_full_reload) {
00631 do_full_reload = 0;
00632 AST_LIST_UNLOCK(&reload_queue);
00633 ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
00634 ast_module_reload(NULL);
00635 return;
00636 }
00637
00638 while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
00639 ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
00640 ast_module_reload(item->module);
00641 ast_free(item);
00642 }
00643
00644 AST_LIST_UNLOCK(&reload_queue);
00645 }
00646
00647 static void queue_reload_request(const char *module)
00648 {
00649 struct reload_queue_item *item;
00650
00651 AST_LIST_LOCK(&reload_queue);
00652
00653 if (do_full_reload) {
00654 AST_LIST_UNLOCK(&reload_queue);
00655 return;
00656 }
00657
00658 if (ast_strlen_zero(module)) {
00659
00660
00661 while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
00662 ast_free(item);
00663 }
00664 do_full_reload = 1;
00665 } else {
00666
00667 AST_LIST_TRAVERSE(&reload_queue, item, entry) {
00668 if (!strcasecmp(item->module, module)) {
00669 AST_LIST_UNLOCK(&reload_queue);
00670 return;
00671 }
00672 }
00673 item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
00674 if (!item) {
00675 ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
00676 AST_LIST_UNLOCK(&reload_queue);
00677 return;
00678 }
00679 strcpy(item->module, module);
00680 AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
00681 }
00682 AST_LIST_UNLOCK(&reload_queue);
00683 }
00684
00685 int ast_module_reload(const char *name)
00686 {
00687 struct ast_module *cur;
00688 int res = 0;
00689 int i;
00690
00691
00692
00693 if (!ast_fully_booted) {
00694 queue_reload_request(name);
00695 return 0;
00696 }
00697
00698 if (ast_mutex_trylock(&reloadlock)) {
00699 ast_verbose("The previous reload command didn't finish yet\n");
00700 return -1;
00701 }
00702 ast_lastreloadtime = ast_tvnow();
00703
00704 if (ast_opt_lock_confdir) {
00705 int try;
00706 int res;
00707 for (try = 1, res = AST_LOCK_TIMEOUT; try < 6 && (res == AST_LOCK_TIMEOUT); try++) {
00708 res = ast_lock_path(ast_config_AST_CONFIG_DIR);
00709 if (res == AST_LOCK_TIMEOUT) {
00710 ast_log(LOG_WARNING, "Failed to grab lock on %s, try %d\n", ast_config_AST_CONFIG_DIR, try);
00711 }
00712 }
00713 if (res != AST_LOCK_SUCCESS) {
00714 ast_verbose("Cannot grab lock on %s\n", ast_config_AST_CONFIG_DIR);
00715 ast_mutex_unlock(&reloadlock);
00716 return -1;
00717 }
00718 }
00719
00720
00721 for (i = 0; reload_classes[i].name; i++) {
00722 if (!name || !strcasecmp(name, reload_classes[i].name)) {
00723 if (!reload_classes[i].reload_fn()) {
00724 ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", name);
00725 }
00726 res = 2;
00727 }
00728 }
00729
00730 if (name && res) {
00731 if (ast_opt_lock_confdir) {
00732 ast_unlock_path(ast_config_AST_CONFIG_DIR);
00733 }
00734 ast_mutex_unlock(&reloadlock);
00735 return res;
00736 }
00737
00738 AST_LIST_LOCK(&module_list);
00739 AST_LIST_TRAVERSE(&module_list, cur, entry) {
00740 const struct ast_module_info *info = cur->info;
00741
00742 if (name && resource_name_match(name, cur->resource))
00743 continue;
00744
00745 if (!cur->flags.running || cur->flags.declined) {
00746 if (!name)
00747 continue;
00748 ast_log(LOG_NOTICE, "The module '%s' was not properly initialized. "
00749 "Before reloading the module, you must run \"module load %s\" "
00750 "and fix whatever is preventing the module from being initialized.\n",
00751 name, name);
00752 res = 2;
00753 break;
00754 }
00755
00756 if (!info->reload) {
00757
00758 ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
00759 if (res < 1)
00760 res = 1;
00761 continue;
00762 }
00763
00764 res = 2;
00765 ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
00766 if (!info->reload()) {
00767 ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
00768 }
00769 }
00770 AST_LIST_UNLOCK(&module_list);
00771
00772 if (ast_opt_lock_confdir) {
00773 ast_unlock_path(ast_config_AST_CONFIG_DIR);
00774 }
00775 ast_mutex_unlock(&reloadlock);
00776
00777 return res;
00778 }
00779
00780 static unsigned int inspect_module(const struct ast_module *mod)
00781 {
00782 if (!mod->info->description) {
00783 ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
00784 return 1;
00785 }
00786
00787 if (!mod->info->key) {
00788 ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
00789 return 1;
00790 }
00791
00792 if (verify_key((unsigned char *) mod->info->key)) {
00793 ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
00794 return 1;
00795 }
00796
00797 if (!ast_strlen_zero(mod->info->buildopt_sum) &&
00798 strcmp(buildopt_sum, mod->info->buildopt_sum)) {
00799 ast_log(LOG_WARNING, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
00800 ast_log(LOG_WARNING, "Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
00801 return 1;
00802 }
00803
00804 return 0;
00805 }
00806
00807 static enum ast_module_load_result start_resource(struct ast_module *mod)
00808 {
00809 char tmp[256];
00810 enum ast_module_load_result res;
00811
00812 if (mod->flags.running) {
00813 return AST_MODULE_LOAD_SUCCESS;
00814 }
00815
00816 if (!mod->info->load) {
00817 return AST_MODULE_LOAD_FAILURE;
00818 }
00819
00820 res = mod->info->load();
00821
00822 switch (res) {
00823 case AST_MODULE_LOAD_SUCCESS:
00824 if (!ast_fully_booted) {
00825 ast_verb(1, "%s => (%s)\n", mod->resource, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
00826 if (ast_opt_console && !option_verbose)
00827 ast_verbose( ".");
00828 } else {
00829 ast_verb(1, "Loaded %s => (%s)\n", mod->resource, mod->info->description);
00830 }
00831
00832 mod->flags.running = 1;
00833
00834 ast_update_use_count();
00835 break;
00836 case AST_MODULE_LOAD_DECLINE:
00837 mod->flags.declined = 1;
00838 break;
00839 case AST_MODULE_LOAD_FAILURE:
00840 case AST_MODULE_LOAD_SKIP:
00841 case AST_MODULE_LOAD_PRIORITY:
00842 break;
00843 }
00844
00845 return res;
00846 }
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857 static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required)
00858 {
00859 struct ast_module *mod;
00860 enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;
00861
00862 if ((mod = find_resource(resource_name, 0))) {
00863 if (mod->flags.running) {
00864 ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
00865 return AST_MODULE_LOAD_DECLINE;
00866 }
00867 if (global_symbols_only && !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS))
00868 return AST_MODULE_LOAD_SKIP;
00869 } else {
00870 #ifdef LOADABLE_MODULES
00871 if (!(mod = load_dynamic_module(resource_name, global_symbols_only, resource_heap))) {
00872
00873 if (!global_symbols_only) {
00874 ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00875 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
00876 } else {
00877 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SKIP;
00878 }
00879 }
00880 #else
00881 ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00882 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
00883 #endif
00884 }
00885
00886 if (inspect_module(mod)) {
00887 ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
00888 #ifdef LOADABLE_MODULES
00889 unload_dynamic_module(mod);
00890 #endif
00891 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
00892 }
00893
00894 if (!mod->lib && mod->info->backup_globals && mod->info->backup_globals()) {
00895 ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
00896 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
00897 }
00898
00899 mod->flags.declined = 0;
00900
00901 if (resource_heap) {
00902 ast_heap_push(resource_heap, mod);
00903 res = AST_MODULE_LOAD_PRIORITY;
00904 } else {
00905 res = start_resource(mod);
00906 }
00907
00908 return res;
00909 }
00910
00911 int ast_load_resource(const char *resource_name)
00912 {
00913 int res;
00914 AST_LIST_LOCK(&module_list);
00915 res = load_resource(resource_name, 0, NULL, 0);
00916 if (!res) {
00917 ast_test_suite_event_notify("MODULE_LOAD", "Message: %s", resource_name);
00918 }
00919 AST_LIST_UNLOCK(&module_list);
00920
00921 return res;
00922 }
00923
00924 struct load_order_entry {
00925 char *resource;
00926 int required;
00927 AST_LIST_ENTRY(load_order_entry) entry;
00928 };
00929
00930 AST_LIST_HEAD_NOLOCK(load_order, load_order_entry);
00931
00932 static struct load_order_entry *add_to_load_order(const char *resource, struct load_order *load_order, int required)
00933 {
00934 struct load_order_entry *order;
00935
00936 AST_LIST_TRAVERSE(load_order, order, entry) {
00937 if (!resource_name_match(order->resource, resource)) {
00938
00939
00940 order->required |= required;
00941 return NULL;
00942 }
00943 }
00944
00945 if (!(order = ast_calloc(1, sizeof(*order))))
00946 return NULL;
00947
00948 order->resource = ast_strdup(resource);
00949 order->required = required;
00950 AST_LIST_INSERT_TAIL(load_order, order, entry);
00951
00952 return order;
00953 }
00954
00955 static int mod_load_cmp(void *a, void *b)
00956 {
00957 struct ast_module *a_mod = (struct ast_module *) a;
00958 struct ast_module *b_mod = (struct ast_module *) b;
00959 int res = -1;
00960
00961 unsigned char a_pri = ast_test_flag(a_mod->info, AST_MODFLAG_LOAD_ORDER) ? a_mod->info->load_pri : 128;
00962 unsigned char b_pri = ast_test_flag(b_mod->info, AST_MODFLAG_LOAD_ORDER) ? b_mod->info->load_pri : 128;
00963 if (a_pri == b_pri) {
00964 res = 0;
00965 } else if (a_pri < b_pri) {
00966 res = 1;
00967 }
00968 return res;
00969 }
00970
00971
00972
00973
00974 static int load_resource_list(struct load_order *load_order, unsigned int global_symbols, int *mod_count)
00975 {
00976 struct ast_heap *resource_heap;
00977 struct load_order_entry *order;
00978 struct ast_module *mod;
00979 int count = 0;
00980 int res = 0;
00981
00982 if(!(resource_heap = ast_heap_create(8, mod_load_cmp, -1))) {
00983 return -1;
00984 }
00985
00986
00987 AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
00988 switch (load_resource(order->resource, global_symbols, resource_heap, order->required)) {
00989 case AST_MODULE_LOAD_SUCCESS:
00990 case AST_MODULE_LOAD_DECLINE:
00991 AST_LIST_REMOVE_CURRENT(entry);
00992 ast_free(order->resource);
00993 ast_free(order);
00994 break;
00995 case AST_MODULE_LOAD_FAILURE:
00996 ast_log(LOG_ERROR, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
00997 fprintf(stderr, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
00998 res = order->required ? -2 : -1;
00999 goto done;
01000 case AST_MODULE_LOAD_SKIP:
01001 break;
01002 case AST_MODULE_LOAD_PRIORITY:
01003 AST_LIST_REMOVE_CURRENT(entry);
01004 break;
01005 }
01006 }
01007 AST_LIST_TRAVERSE_SAFE_END;
01008
01009
01010 while ((mod = ast_heap_pop(resource_heap))) {
01011 switch (start_resource(mod)) {
01012 case AST_MODULE_LOAD_SUCCESS:
01013 count++;
01014 case AST_MODULE_LOAD_DECLINE:
01015 break;
01016 case AST_MODULE_LOAD_FAILURE:
01017 res = -1;
01018 goto done;
01019 case AST_MODULE_LOAD_SKIP:
01020 case AST_MODULE_LOAD_PRIORITY:
01021 break;
01022 }
01023 }
01024
01025 done:
01026 if (mod_count) {
01027 *mod_count += count;
01028 }
01029 ast_heap_destroy(resource_heap);
01030
01031 return res;
01032 }
01033
01034 int load_modules(unsigned int preload_only)
01035 {
01036 struct ast_config *cfg;
01037 struct ast_module *mod;
01038 struct load_order_entry *order;
01039 struct ast_variable *v;
01040 unsigned int load_count;
01041 struct load_order load_order;
01042 int res = 0;
01043 struct ast_flags config_flags = { 0 };
01044 int modulecount = 0;
01045
01046 #ifdef LOADABLE_MODULES
01047 struct dirent *dirent;
01048 DIR *dir;
01049 #endif
01050
01051
01052 embedding = 0;
01053
01054 ast_verb(1, "Asterisk Dynamic Loader Starting:\n");
01055
01056 AST_LIST_HEAD_INIT_NOLOCK(&load_order);
01057
01058 AST_LIST_LOCK(&module_list);
01059
01060 if (embedded_module_list.first) {
01061 module_list.first = embedded_module_list.first;
01062 module_list.last = embedded_module_list.last;
01063 embedded_module_list.first = NULL;
01064 }
01065
01066 cfg = ast_config_load2(AST_MODULE_CONFIG, "" , config_flags);
01067 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
01068 ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
01069 goto done;
01070 }
01071
01072
01073 for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
01074 if (!strcasecmp(v->name, preload_only ? "preload" : "load")) {
01075 add_to_load_order(v->value, &load_order, 0);
01076 }
01077 if (!strcasecmp(v->name, preload_only ? "preload-require" : "require")) {
01078
01079 add_to_load_order(v->value, &load_order, 1);
01080 ast_debug(2, "Adding module to required list: %s (%s)\n", v->value, v->name);
01081 }
01082
01083 }
01084
01085
01086 if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
01087
01088 AST_LIST_TRAVERSE(&module_list, mod, entry) {
01089
01090 if (mod->lib)
01091 continue;
01092
01093 if (mod->flags.running)
01094 continue;
01095
01096 add_to_load_order(mod->resource, &load_order, 0);
01097 }
01098
01099 #ifdef LOADABLE_MODULES
01100
01101
01102 if ((dir = opendir(ast_config_AST_MODULE_DIR))) {
01103 while ((dirent = readdir(dir))) {
01104 int ld = strlen(dirent->d_name);
01105
01106
01107
01108 if (ld < 4)
01109 continue;
01110
01111 if (strcasecmp(dirent->d_name + ld - 3, ".so"))
01112 continue;
01113
01114
01115
01116 if (find_resource(dirent->d_name, 0))
01117 continue;
01118
01119 add_to_load_order(dirent->d_name, &load_order, 0);
01120 }
01121
01122 closedir(dir);
01123 } else {
01124 if (!ast_opt_quiet)
01125 ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
01126 ast_config_AST_MODULE_DIR);
01127 }
01128 #endif
01129 }
01130
01131
01132
01133 for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
01134 if (strcasecmp(v->name, "noload"))
01135 continue;
01136
01137 AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
01138 if (!resource_name_match(order->resource, v->value)) {
01139 AST_LIST_REMOVE_CURRENT(entry);
01140 ast_free(order->resource);
01141 ast_free(order);
01142 }
01143 }
01144 AST_LIST_TRAVERSE_SAFE_END;
01145 }
01146
01147
01148
01149 ast_config_destroy(cfg);
01150
01151 load_count = 0;
01152 AST_LIST_TRAVERSE(&load_order, order, entry)
01153 load_count++;
01154
01155 if (load_count)
01156 ast_log(LOG_NOTICE, "%d modules will be loaded.\n", load_count);
01157
01158
01159 if ((res = load_resource_list(&load_order, 1, &modulecount)) < 0) {
01160 goto done;
01161 }
01162
01163
01164 if ((res = load_resource_list(&load_order, 0, &modulecount)) < 0) {
01165 goto done;
01166 }
01167
01168 done:
01169 while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
01170 ast_free(order->resource);
01171 ast_free(order);
01172 }
01173
01174 AST_LIST_UNLOCK(&module_list);
01175
01176
01177
01178
01179 manager_event(EVENT_FLAG_SYSTEM, "ModuleLoadReport", "ModuleLoadStatus: Done\r\nModuleSelection: %s\r\nModuleCount: %d\r\n", preload_only ? "Preload" : "All", modulecount);
01180
01181 return res;
01182 }
01183
01184 void ast_update_use_count(void)
01185 {
01186
01187
01188 struct loadupdate *m;
01189
01190 AST_LIST_LOCK(&updaters);
01191 AST_LIST_TRAVERSE(&updaters, m, entry)
01192 m->updater();
01193 AST_LIST_UNLOCK(&updaters);
01194 }
01195
01196 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
01197 const char *like)
01198 {
01199 struct ast_module *cur;
01200 int unlock = -1;
01201 int total_mod_loaded = 0;
01202
01203 if (AST_LIST_TRYLOCK(&module_list))
01204 unlock = 0;
01205
01206 AST_LIST_TRAVERSE(&module_list, cur, entry) {
01207 total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
01208 }
01209
01210 if (unlock)
01211 AST_LIST_UNLOCK(&module_list);
01212
01213 return total_mod_loaded;
01214 }
01215
01216
01217 int ast_module_check(const char *name)
01218 {
01219 struct ast_module *cur;
01220
01221 if (ast_strlen_zero(name))
01222 return 0;
01223
01224 cur = find_resource(name, 1);
01225
01226 return (cur != NULL);
01227 }
01228
01229
01230 int ast_loader_register(int (*v)(void))
01231 {
01232 struct loadupdate *tmp;
01233
01234 if (!(tmp = ast_malloc(sizeof(*tmp))))
01235 return -1;
01236
01237 tmp->updater = v;
01238 AST_LIST_LOCK(&updaters);
01239 AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
01240 AST_LIST_UNLOCK(&updaters);
01241
01242 return 0;
01243 }
01244
01245 int ast_loader_unregister(int (*v)(void))
01246 {
01247 struct loadupdate *cur;
01248
01249 AST_LIST_LOCK(&updaters);
01250 AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
01251 if (cur->updater == v) {
01252 AST_LIST_REMOVE_CURRENT(entry);
01253 break;
01254 }
01255 }
01256 AST_LIST_TRAVERSE_SAFE_END;
01257 AST_LIST_UNLOCK(&updaters);
01258
01259 return cur ? 0 : -1;
01260 }
01261
01262 struct ast_module *ast_module_ref(struct ast_module *mod)
01263 {
01264 if (!mod) {
01265 return NULL;
01266 }
01267
01268 ast_atomic_fetchadd_int(&mod->usecount, +1);
01269 ast_update_use_count();
01270
01271 return mod;
01272 }
01273
01274 void ast_module_unref(struct ast_module *mod)
01275 {
01276 if (!mod) {
01277 return;
01278 }
01279
01280 ast_atomic_fetchadd_int(&mod->usecount, -1);
01281 ast_update_use_count();
01282 }