📄 loader.c
字号:
} m->lib = dlopen(fn, flags); if (!m->lib) { ast_log(LOG_WARNING, "%s\n", dlerror()); free(m); ast_mutex_unlock(&modlock); return -1; } m->load_module = dlsym(m->lib, "load_module"); if (m->load_module == NULL) m->load_module = dlsym(m->lib, "_load_module"); if (!m->load_module) { ast_log(LOG_WARNING, "No load_module in module %s\n", fn); errors++; } m->unload_module = dlsym(m->lib, "unload_module"); if (m->unload_module == NULL) m->unload_module = dlsym(m->lib, "_unload_module"); if (!m->unload_module) { ast_log(LOG_WARNING, "No unload_module in module %s\n", fn); errors++; } m->usecount = dlsym(m->lib, "usecount"); if (m->usecount == NULL) m->usecount = dlsym(m->lib, "_usecount"); if (!m->usecount) { ast_log(LOG_WARNING, "No usecount in module %s\n", fn); errors++; } m->description = dlsym(m->lib, "description"); if (m->description == NULL) m->description = dlsym(m->lib, "_description"); if (!m->description) { ast_log(LOG_WARNING, "No description in module %s\n", fn); errors++; } m->key = dlsym(m->lib, "key"); if (m->key == NULL) m->key = dlsym(m->lib, "_key"); if (!m->key) { ast_log(LOG_WARNING, "No key routine in module %s\n", fn); errors++; } m->reload = dlsym(m->lib, "reload"); if (m->reload == NULL) m->reload = dlsym(m->lib, "_reload"); if (!m->key || !(key = (unsigned char *) m->key())) { ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn); key = NULL; errors++; } if (key && verify_key(key)) { ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn); errors++; } if (errors) { ast_log(LOG_WARNING, "%d error%s loading module %s, aborted\n", errors, (errors != 1) ? "s" : "", fn); dlclose(m->lib); free(m); ast_mutex_unlock(&modlock); return -1; } if (!fully_booted) { if (option_verbose) ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp))); if (option_console && !option_verbose) ast_verbose( "."); } else { if (option_verbose) ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description()); } /* add module 'm' to end of module_list chain so reload commands will be issued in same order modules were loaded */ m->next = NULL; if (module_list == NULL) { /* empty list so far, add at front */ module_list = m; } else { struct module *i; /* find end of chain, and add there */ for (i = module_list; i->next; i = i->next) ; i->next = m; } modlistver = rand(); ast_mutex_unlock(&modlock); if ((res = m->load_module())) { ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res); ast_unload_resource(resource_name, 0); return -1; } ast_update_use_count(); return 0;}int ast_load_resource(const char *resource_name){ int o; struct ast_config *cfg = NULL; int res; /* Keep the module file parsing silent */ o = option_verbose; option_verbose = 0; cfg = ast_config_load(AST_MODULE_CONFIG); option_verbose = o; res = __load_resource(resource_name, cfg); if (cfg) ast_config_destroy(cfg); return res;} static int ast_resource_exists(char *resource){ struct module *m; if (ast_mutex_lock(&modlock)) ast_log(LOG_WARNING, "Failed to lock\n"); m = module_list; while(m) { if (!strcasecmp(resource, m->resource)) break; m = m->next; } ast_mutex_unlock(&modlock); if (m) return -1; else return 0;}static const char *loadorder[] ={ "res_", "pbx_", "chan_", NULL,};int load_modules(const int preload_only){ struct ast_config *cfg; struct ast_variable *v; char tmp[80]; if (option_verbose) { if (preload_only) ast_verbose("Asterisk Dynamic Loader loading preload modules:\n"); else ast_verbose("Asterisk Dynamic Loader Starting:\n"); } cfg = ast_config_load(AST_MODULE_CONFIG); if (cfg) { int doload; /* Load explicitly defined modules */ for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) { doload = 0; if (preload_only) doload = !strcasecmp(v->name, "preload"); else doload = !strcasecmp(v->name, "load"); if (doload) { if (option_debug && !option_verbose) ast_log(LOG_DEBUG, "Loading module %s\n", v->value); if (option_verbose) { ast_verbose(VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp))); fflush(stdout); } if (__load_resource(v->value, cfg)) { ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value); ast_config_destroy(cfg); return -1; } } } } if (preload_only) { ast_config_destroy(cfg); return 0; } if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) { /* Load all modules */ DIR *mods; struct dirent *d; int x; /* Loop through each order */ for (x=0; x<sizeof(loadorder) / sizeof(loadorder[0]); x++) { mods = opendir((char *)ast_config_AST_MODULE_DIR); if (mods) { while((d = readdir(mods))) { /* Must end in .so to load it. */ if ((strlen(d->d_name) > 3) && (!loadorder[x] || !strncasecmp(d->d_name, loadorder[x], strlen(loadorder[x]))) && !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") && !ast_resource_exists(d->d_name)) { /* It's a shared library -- Just be sure we're allowed to load it -- kinda an inefficient way to do it, but oh well. */ if (cfg) { v = ast_variable_browse(cfg, "modules"); while(v) { if (!strcasecmp(v->name, "noload") && !strcasecmp(v->value, d->d_name)) break; v = v->next; } if (v) { if (option_verbose) { ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name); fflush(stdout); } continue; } } if (option_debug && !option_verbose) ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name); if (option_verbose) { ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp))); fflush(stdout); } if (__load_resource(d->d_name, cfg)) { ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name); if (cfg) ast_config_destroy(cfg); return -1; } } } closedir(mods); } else { if (!option_quiet) ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR); } } } ast_config_destroy(cfg); return 0;}void ast_update_use_count(void){ /* Notify any module monitors that the use count for a resource has changed */ struct loadupdate *m; if (ast_mutex_lock(&modlock)) ast_log(LOG_WARNING, "Failed to lock\n"); m = updaters; while(m) { m->updater(); m = m->next; } ast_mutex_unlock(&modlock); }int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like), const char *like){ struct module *m; int unlock = -1; int total_mod_loaded = 0; if (ast_mutex_trylock(&modlock)) unlock = 0; m = module_list; while (m) { total_mod_loaded += modentry(m->resource, m->description(), m->usecount(), like); m = m->next; } if (unlock) ast_mutex_unlock(&modlock); return total_mod_loaded;}int ast_loader_register(int (*v)(void)) { struct loadupdate *tmp; /* XXX Should be more flexible here, taking > 1 verboser XXX */ if ((tmp = malloc(sizeof (struct loadupdate)))) { tmp->updater = v; if (ast_mutex_lock(&modlock)) ast_log(LOG_WARNING, "Failed to lock\n"); tmp->next = updaters; updaters = tmp; ast_mutex_unlock(&modlock); return 0; } return -1;}int ast_loader_unregister(int (*v)(void)){ int res = -1; struct loadupdate *tmp, *tmpl=NULL; if (ast_mutex_lock(&modlock)) ast_log(LOG_WARNING, "Failed to lock\n"); tmp = updaters; while(tmp) { if (tmp->updater == v) { if (tmpl) tmpl->next = tmp->next; else updaters = tmp->next; break; } tmpl = tmp; tmp = tmp->next; } if (tmp) res = 0; ast_mutex_unlock(&modlock); return res;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -