loader.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Kevin P. Fleming <kpfleming@digium.com>
  8. * Luigi Rizzo <rizzo@icir.org>
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief Module Loader
  23. * \author Mark Spencer <markster@digium.com>
  24. * \author Kevin P. Fleming <kpfleming@digium.com>
  25. * \author Luigi Rizzo <rizzo@icir.org>
  26. * - See ModMngMnt
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/_private.h"
  34. #include "asterisk/paths.h" /* use ast_config_AST_MODULE_DIR */
  35. #include <dirent.h>
  36. #include "asterisk/linkedlists.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/config.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/term.h"
  41. #include "asterisk/acl.h"
  42. #include "asterisk/manager.h"
  43. #include "asterisk/cdr.h"
  44. #include "asterisk/enum.h"
  45. #include "asterisk/http.h"
  46. #include "asterisk/lock.h"
  47. #include "asterisk/features.h"
  48. #include "asterisk/dsp.h"
  49. #include "asterisk/udptl.h"
  50. #include "asterisk/heap.h"
  51. #include "asterisk/app.h"
  52. #include "asterisk/test.h"
  53. #include <dlfcn.h>
  54. #include "asterisk/md5.h"
  55. #include "asterisk/utils.h"
  56. /*** DOCUMENTATION
  57. ***/
  58. #ifndef RTLD_NOW
  59. #define RTLD_NOW 0
  60. #endif
  61. #ifndef RTLD_LOCAL
  62. #define RTLD_LOCAL 0
  63. #endif
  64. struct ast_module_user {
  65. struct ast_channel *chan;
  66. AST_LIST_ENTRY(ast_module_user) entry;
  67. };
  68. AST_LIST_HEAD(module_user_list, ast_module_user);
  69. static const unsigned char expected_key[] =
  70. { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
  71. 0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
  72. static char buildopt_sum[33] = AST_BUILDOPT_SUM;
  73. static unsigned int embedding = 1; /* we always start out by registering embedded modules,
  74. since they are here before we dlopen() any
  75. */
  76. struct ast_module {
  77. const struct ast_module_info *info;
  78. #ifdef REF_DEBUG
  79. /* Used to get module references into REF_DEBUG logs */
  80. void *ref_debug;
  81. #endif
  82. void *lib; /* the shared lib, or NULL if embedded */
  83. int usecount; /* the number of 'users' currently in this module */
  84. struct module_user_list users; /* the list of users in the module */
  85. struct {
  86. unsigned int running:1;
  87. unsigned int declined:1;
  88. unsigned int keepuntilshutdown:1;
  89. } flags;
  90. AST_LIST_ENTRY(ast_module) entry;
  91. char resource[0];
  92. };
  93. static AST_LIST_HEAD_STATIC(module_list, ast_module);
  94. const char *ast_module_name(const struct ast_module *mod)
  95. {
  96. if (!mod || !mod->info) {
  97. return NULL;
  98. }
  99. return mod->info->name;
  100. }
  101. /*
  102. * module_list is cleared by its constructor possibly after
  103. * we start accumulating embedded modules, so we need to
  104. * use another list (without the lock) to accumulate them.
  105. * Then we update the main list when embedding is done.
  106. */
  107. static struct module_list embedded_module_list;
  108. struct loadupdate {
  109. int (*updater)(void);
  110. AST_LIST_ENTRY(loadupdate) entry;
  111. };
  112. static AST_LIST_HEAD_STATIC(updaters, loadupdate);
  113. AST_MUTEX_DEFINE_STATIC(reloadlock);
  114. struct reload_queue_item {
  115. AST_LIST_ENTRY(reload_queue_item) entry;
  116. char module[0];
  117. };
  118. static int do_full_reload = 0;
  119. static AST_LIST_HEAD_STATIC(reload_queue, reload_queue_item);
  120. /* when dynamic modules are being loaded, ast_module_register() will
  121. need to know what filename the module was loaded from while it
  122. is being registered
  123. */
  124. static struct ast_module *resource_being_loaded;
  125. /* XXX: should we check for duplicate resource names here? */
  126. void ast_module_register(const struct ast_module_info *info)
  127. {
  128. struct ast_module *mod;
  129. if (embedding) {
  130. if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
  131. return;
  132. strcpy(mod->resource, info->name);
  133. } else {
  134. mod = resource_being_loaded;
  135. }
  136. mod->info = info;
  137. #ifdef REF_DEBUG
  138. mod->ref_debug = ao2_t_alloc(0, NULL, info->name);
  139. #endif
  140. AST_LIST_HEAD_INIT(&mod->users);
  141. /* during startup, before the loader has been initialized,
  142. there are no threads, so there is no need to take the lock
  143. on this list to manipulate it. it is also possible that it
  144. might be unsafe to use the list lock at that point... so
  145. let's avoid it altogether
  146. */
  147. if (embedding) {
  148. AST_LIST_INSERT_TAIL(&embedded_module_list, mod, entry);
  149. } else {
  150. AST_LIST_LOCK(&module_list);
  151. /* it is paramount that the new entry be placed at the tail of
  152. the list, otherwise the code that uses dlopen() to load
  153. dynamic modules won't be able to find out if the module it
  154. just opened was registered or failed to load
  155. */
  156. AST_LIST_INSERT_TAIL(&module_list, mod, entry);
  157. AST_LIST_UNLOCK(&module_list);
  158. }
  159. /* give the module a copy of its own handle, for later use in registrations and the like */
  160. *((struct ast_module **) &(info->self)) = mod;
  161. }
  162. void ast_module_unregister(const struct ast_module_info *info)
  163. {
  164. struct ast_module *mod = NULL;
  165. /* it is assumed that the users list in the module structure
  166. will already be empty, or we cannot have gotten to this
  167. point
  168. */
  169. AST_LIST_LOCK(&module_list);
  170. AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
  171. if (mod->info == info) {
  172. AST_LIST_REMOVE_CURRENT(entry);
  173. break;
  174. }
  175. }
  176. AST_LIST_TRAVERSE_SAFE_END;
  177. AST_LIST_UNLOCK(&module_list);
  178. if (mod) {
  179. AST_LIST_HEAD_DESTROY(&mod->users);
  180. #ifdef REF_DEBUG
  181. ao2_cleanup(mod->ref_debug);
  182. #endif
  183. ast_free(mod);
  184. }
  185. }
  186. struct ast_module_user *__ast_module_user_add(struct ast_module *mod, struct ast_channel *chan)
  187. {
  188. struct ast_module_user *u;
  189. u = ast_calloc(1, sizeof(*u));
  190. if (!u) {
  191. return NULL;
  192. }
  193. u->chan = chan;
  194. AST_LIST_LOCK(&mod->users);
  195. AST_LIST_INSERT_HEAD(&mod->users, u, entry);
  196. AST_LIST_UNLOCK(&mod->users);
  197. #ifdef REF_DEBUG
  198. ao2_ref(mod->ref_debug, +1);
  199. #endif
  200. ast_atomic_fetchadd_int(&mod->usecount, +1);
  201. ast_update_use_count();
  202. return u;
  203. }
  204. void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
  205. {
  206. if (!u) {
  207. return;
  208. }
  209. AST_LIST_LOCK(&mod->users);
  210. u = AST_LIST_REMOVE(&mod->users, u, entry);
  211. AST_LIST_UNLOCK(&mod->users);
  212. if (!u) {
  213. /*
  214. * Was not in the list. Either a bad pointer or
  215. * __ast_module_user_hangup_all() has been called.
  216. */
  217. return;
  218. }
  219. #ifdef REF_DEBUG
  220. ao2_ref(mod->ref_debug, -1);
  221. #endif
  222. ast_atomic_fetchadd_int(&mod->usecount, -1);
  223. ast_free(u);
  224. ast_update_use_count();
  225. }
  226. void __ast_module_user_hangup_all(struct ast_module *mod)
  227. {
  228. struct ast_module_user *u;
  229. AST_LIST_LOCK(&mod->users);
  230. while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
  231. if (u->chan) {
  232. ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
  233. }
  234. #ifdef REF_DEBUG
  235. ao2_ref(mod->ref_debug, -1);
  236. #endif
  237. ast_atomic_fetchadd_int(&mod->usecount, -1);
  238. ast_free(u);
  239. }
  240. AST_LIST_UNLOCK(&mod->users);
  241. ast_update_use_count();
  242. }
  243. /*! \note
  244. * In addition to modules, the reload command handles some extra keywords
  245. * which are listed here together with the corresponding handlers.
  246. * This table is also used by the command completion code.
  247. */
  248. static struct reload_classes {
  249. const char *name;
  250. int (*reload_fn)(void);
  251. } reload_classes[] = { /* list in alpha order, longest match first for cli completion */
  252. { "cdr", ast_cdr_engine_reload },
  253. { "dnsmgr", dnsmgr_reload },
  254. { "extconfig", read_config_maps },
  255. { "enum", ast_enum_reload },
  256. { "acl", ast_named_acl_reload },
  257. { "manager", reload_manager },
  258. { "http", ast_http_reload },
  259. { "logger", logger_reload },
  260. { "features", ast_features_reload },
  261. { "dsp", ast_dsp_reload},
  262. { "udptl", ast_udptl_reload },
  263. { "indications", ast_indications_reload },
  264. { "cel", ast_cel_engine_reload },
  265. { "plc", ast_plc_reload },
  266. { NULL, NULL }
  267. };
  268. static int printdigest(const unsigned char *d)
  269. {
  270. int x, pos;
  271. char buf[256]; /* large enough so we don't have to worry */
  272. for (pos = 0, x = 0; x < 16; x++)
  273. pos += sprintf(buf + pos, " %02hhx", *d++);
  274. ast_debug(1, "Unexpected signature:%s\n", buf);
  275. return 0;
  276. }
  277. static int key_matches(const unsigned char *key1, const unsigned char *key2)
  278. {
  279. int x;
  280. for (x = 0; x < 16; x++) {
  281. if (key1[x] != key2[x])
  282. return 0;
  283. }
  284. return 1;
  285. }
  286. static int verify_key(const unsigned char *key)
  287. {
  288. struct MD5Context c;
  289. unsigned char digest[16];
  290. MD5Init(&c);
  291. MD5Update(&c, key, strlen((char *)key));
  292. MD5Final(digest, &c);
  293. if (key_matches(expected_key, digest))
  294. return 0;
  295. printdigest(digest);
  296. return -1;
  297. }
  298. static int resource_name_match(const char *name1_in, const char *name2_in)
  299. {
  300. char *name1 = (char *) name1_in;
  301. char *name2 = (char *) name2_in;
  302. /* trim off any .so extensions */
  303. if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
  304. name1 = ast_strdupa(name1);
  305. name1[strlen(name1) - 3] = '\0';
  306. }
  307. if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
  308. name2 = ast_strdupa(name2);
  309. name2[strlen(name2) - 3] = '\0';
  310. }
  311. return strcasecmp(name1, name2);
  312. }
  313. static struct ast_module *find_resource(const char *resource, int do_lock)
  314. {
  315. struct ast_module *cur;
  316. if (do_lock)
  317. AST_LIST_LOCK(&module_list);
  318. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  319. if (!resource_name_match(resource, cur->resource))
  320. break;
  321. }
  322. if (do_lock)
  323. AST_LIST_UNLOCK(&module_list);
  324. return cur;
  325. }
  326. #ifdef LOADABLE_MODULES
  327. static void close_lib(const char *name, void *lib)
  328. {
  329. char *error;
  330. if (!lib) {
  331. return;
  332. }
  333. /* Clear any existing error */
  334. dlerror();
  335. if (dlclose(lib)) {
  336. error = dlerror();
  337. ast_log(AST_LOG_ERROR, "Failure in dlclose for module '%s': %s\n",
  338. S_OR(name, "unknown"), S_OR(error, "Unknown error"));
  339. }
  340. }
  341. static void unload_dynamic_module(struct ast_module *mod)
  342. {
  343. void *lib = mod->lib;
  344. /* WARNING: the structure pointed to by mod is going to
  345. disappear when this operation succeeds, so we can't
  346. dereference it */
  347. close_lib(ast_module_name(mod), lib);
  348. }
  349. static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required);
  350. static struct ast_module *load_dynamic_module(const char *resource_in, unsigned int global_symbols_only, struct ast_heap *resource_heap)
  351. {
  352. char fn[PATH_MAX] = "";
  353. void *lib = NULL;
  354. struct ast_module *mod;
  355. unsigned int wants_global;
  356. int space; /* room needed for the descriptor */
  357. int missing_so = 0;
  358. space = sizeof(*resource_being_loaded) + strlen(resource_in) + 1;
  359. if (strcasecmp(resource_in + strlen(resource_in) - 3, ".so")) {
  360. missing_so = 1;
  361. space += 3; /* room for the extra ".so" */
  362. }
  363. snprintf(fn, sizeof(fn), "%s/%s%s", ast_config_AST_MODULE_DIR, resource_in, missing_so ? ".so" : "");
  364. /* make a first load of the module in 'quiet' mode... don't try to resolve
  365. any symbols, and don't export any symbols. this will allow us to peek into
  366. the module's info block (if available) to see what flags it has set */
  367. resource_being_loaded = ast_calloc(1, space);
  368. if (!resource_being_loaded)
  369. return NULL;
  370. strcpy(resource_being_loaded->resource, resource_in);
  371. if (missing_so)
  372. strcat(resource_being_loaded->resource, ".so");
  373. if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_LOCAL))) {
  374. ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
  375. ast_free(resource_being_loaded);
  376. return NULL;
  377. }
  378. /* the dlopen() succeeded, let's find out if the module
  379. registered itself */
  380. /* note that this will only work properly as long as
  381. ast_module_register() (which is called by the module's
  382. constructor) places the new module at the tail of the
  383. module_list
  384. */
  385. if (resource_being_loaded != (mod = AST_LIST_LAST(&module_list))) {
  386. ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
  387. /* no, it did not, so close it and return */
  388. close_lib(resource_in, lib);
  389. /* note that the module's destructor will call ast_module_unregister(),
  390. which will free the structure we allocated in resource_being_loaded */
  391. return NULL;
  392. }
  393. wants_global = ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS);
  394. /* if we are being asked only to load modules that provide global symbols,
  395. and this one does not, then close it and return */
  396. if (global_symbols_only && !wants_global) {
  397. close_lib(resource_in, lib);
  398. return NULL;
  399. }
  400. /* This section is a workaround for a gcc 4.1 bug that has already been
  401. * fixed in later versions. Unfortunately, some distributions, such as
  402. * RHEL/CentOS 5, distribute gcc 4.1, so we're stuck with having to deal
  403. * with this issue. This basically ensures that optional_api modules are
  404. * loaded before any module which requires their functionality. */
  405. #if !defined(HAVE_ATTRIBUTE_weak_import) && !defined(HAVE_ATTRIBUTE_weakref)
  406. if (!ast_strlen_zero(mod->info->nonoptreq)) {
  407. /* Force any required dependencies to load */
  408. char *each, *required_resource = ast_strdupa(mod->info->nonoptreq);
  409. while ((each = strsep(&required_resource, ","))) {
  410. struct ast_module *dependency;
  411. each = ast_strip(each);
  412. dependency = find_resource(each, 0);
  413. /* Is it already loaded? */
  414. if (!dependency) {
  415. load_resource(each, global_symbols_only, resource_heap, 1);
  416. }
  417. }
  418. }
  419. #endif
  420. close_lib(resource_in, lib);
  421. resource_being_loaded = NULL;
  422. /* start the load process again */
  423. resource_being_loaded = ast_calloc(1, space);
  424. if (!resource_being_loaded)
  425. return NULL;
  426. strcpy(resource_being_loaded->resource, resource_in);
  427. if (missing_so)
  428. strcat(resource_being_loaded->resource, ".so");
  429. if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
  430. ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
  431. ast_free(resource_being_loaded);
  432. return NULL;
  433. }
  434. /* since the module was successfully opened, and it registered itself
  435. the previous time we did that, we're going to assume it worked this
  436. time too :) */
  437. AST_LIST_LAST(&module_list)->lib = lib;
  438. resource_being_loaded = NULL;
  439. return AST_LIST_LAST(&module_list);
  440. }
  441. #endif
  442. void ast_module_shutdown(void)
  443. {
  444. struct ast_module *mod;
  445. int somethingchanged = 1, final = 0;
  446. AST_LIST_LOCK(&module_list);
  447. /*!\note Some resources, like timers, are started up dynamically, and thus
  448. * may be still in use, even if all channels are dead. We must therefore
  449. * check the usecount before asking modules to unload. */
  450. do {
  451. if (!somethingchanged) {
  452. /*!\note If we go through the entire list without changing
  453. * anything, ignore the usecounts and unload, then exit. */
  454. final = 1;
  455. }
  456. /* Reset flag before traversing the list */
  457. somethingchanged = 0;
  458. AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
  459. if (!final && mod->usecount) {
  460. continue;
  461. }
  462. AST_LIST_REMOVE_CURRENT(entry);
  463. if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
  464. mod->info->unload();
  465. }
  466. AST_LIST_HEAD_DESTROY(&mod->users);
  467. #ifdef REF_DEBUG
  468. ao2_cleanup(mod->ref_debug);
  469. #endif
  470. free(mod);
  471. somethingchanged = 1;
  472. }
  473. AST_LIST_TRAVERSE_SAFE_END;
  474. if (!somethingchanged) {
  475. AST_LIST_TRAVERSE(&module_list, mod, entry) {
  476. if (mod->flags.keepuntilshutdown) {
  477. ast_module_unref(mod);
  478. mod->flags.keepuntilshutdown = 0;
  479. somethingchanged = 1;
  480. }
  481. }
  482. }
  483. } while (somethingchanged && !final);
  484. AST_LIST_UNLOCK(&module_list);
  485. }
  486. int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
  487. {
  488. struct ast_module *mod;
  489. int res = -1;
  490. int error = 0;
  491. AST_LIST_LOCK(&module_list);
  492. if (!(mod = find_resource(resource_name, 0))) {
  493. AST_LIST_UNLOCK(&module_list);
  494. ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
  495. return -1;
  496. }
  497. if (!mod->flags.running || mod->flags.declined) {
  498. ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
  499. error = 1;
  500. }
  501. if (!error && (mod->usecount > 0)) {
  502. if (force)
  503. ast_log(LOG_WARNING, "Warning: Forcing removal of module '%s' with use count %d\n",
  504. resource_name, mod->usecount);
  505. else {
  506. ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
  507. mod->usecount);
  508. error = 1;
  509. }
  510. }
  511. if (!error) {
  512. /* Request any channels attached to the module to hangup. */
  513. __ast_module_user_hangup_all(mod);
  514. res = mod->info->unload();
  515. if (res) {
  516. ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
  517. if (force <= AST_FORCE_FIRM) {
  518. error = 1;
  519. } else {
  520. ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
  521. }
  522. }
  523. if (!error) {
  524. /*
  525. * Request hangup on any channels that managed to get attached
  526. * while we called the module unload function.
  527. */
  528. __ast_module_user_hangup_all(mod);
  529. sched_yield();
  530. }
  531. }
  532. if (!error)
  533. mod->flags.running = mod->flags.declined = 0;
  534. AST_LIST_UNLOCK(&module_list);
  535. if (!error && !mod->lib && mod->info && mod->info->restore_globals)
  536. mod->info->restore_globals();
  537. #ifdef LOADABLE_MODULES
  538. if (!error) {
  539. unload_dynamic_module(mod);
  540. ast_test_suite_event_notify("MODULE_UNLOAD", "Message: %s", resource_name);
  541. }
  542. #endif
  543. if (!error)
  544. ast_update_use_count();
  545. return res;
  546. }
  547. char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload)
  548. {
  549. struct ast_module *cur;
  550. int i, which=0, l = strlen(word);
  551. char *ret = NULL;
  552. if (pos != rpos)
  553. return NULL;
  554. AST_LIST_LOCK(&module_list);
  555. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  556. if (!strncasecmp(word, cur->resource, l) &&
  557. (cur->info->reload || !needsreload) &&
  558. ++which > state) {
  559. ret = ast_strdup(cur->resource);
  560. break;
  561. }
  562. }
  563. AST_LIST_UNLOCK(&module_list);
  564. if (!ret) {
  565. for (i=0; !ret && reload_classes[i].name; i++) {
  566. if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
  567. ret = ast_strdup(reload_classes[i].name);
  568. }
  569. }
  570. return ret;
  571. }
  572. void ast_process_pending_reloads(void)
  573. {
  574. struct reload_queue_item *item;
  575. if (!ast_fully_booted) {
  576. return;
  577. }
  578. AST_LIST_LOCK(&reload_queue);
  579. if (do_full_reload) {
  580. do_full_reload = 0;
  581. AST_LIST_UNLOCK(&reload_queue);
  582. ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
  583. ast_module_reload(NULL);
  584. return;
  585. }
  586. while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
  587. ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
  588. ast_module_reload(item->module);
  589. ast_free(item);
  590. }
  591. AST_LIST_UNLOCK(&reload_queue);
  592. }
  593. static void queue_reload_request(const char *module)
  594. {
  595. struct reload_queue_item *item;
  596. AST_LIST_LOCK(&reload_queue);
  597. if (do_full_reload) {
  598. AST_LIST_UNLOCK(&reload_queue);
  599. return;
  600. }
  601. if (ast_strlen_zero(module)) {
  602. /* A full reload request (when module is NULL) wipes out any previous
  603. reload requests and causes the queue to ignore future ones */
  604. while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
  605. ast_free(item);
  606. }
  607. do_full_reload = 1;
  608. } else {
  609. /* No reason to add the same module twice */
  610. AST_LIST_TRAVERSE(&reload_queue, item, entry) {
  611. if (!strcasecmp(item->module, module)) {
  612. AST_LIST_UNLOCK(&reload_queue);
  613. return;
  614. }
  615. }
  616. item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
  617. if (!item) {
  618. ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
  619. AST_LIST_UNLOCK(&reload_queue);
  620. return;
  621. }
  622. strcpy(item->module, module);
  623. AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
  624. }
  625. AST_LIST_UNLOCK(&reload_queue);
  626. }
  627. int ast_module_reload(const char *name)
  628. {
  629. struct ast_module *cur;
  630. int res = 0; /* return value. 0 = not found, others, see below */
  631. int i;
  632. /* If we aren't fully booted, we just pretend we reloaded but we queue this
  633. up to run once we are booted up. */
  634. if (!ast_fully_booted) {
  635. queue_reload_request(name);
  636. return 0;
  637. }
  638. if (ast_mutex_trylock(&reloadlock)) {
  639. ast_verbose("The previous reload command didn't finish yet\n");
  640. return -1; /* reload already in progress */
  641. }
  642. ast_lastreloadtime = ast_tvnow();
  643. if (ast_opt_lock_confdir) {
  644. int try;
  645. int res;
  646. for (try = 1, res = AST_LOCK_TIMEOUT; try < 6 && (res == AST_LOCK_TIMEOUT); try++) {
  647. res = ast_lock_path(ast_config_AST_CONFIG_DIR);
  648. if (res == AST_LOCK_TIMEOUT) {
  649. ast_log(LOG_WARNING, "Failed to grab lock on %s, try %d\n", ast_config_AST_CONFIG_DIR, try);
  650. }
  651. }
  652. if (res != AST_LOCK_SUCCESS) {
  653. ast_verbose("Cannot grab lock on %s\n", ast_config_AST_CONFIG_DIR);
  654. ast_mutex_unlock(&reloadlock);
  655. return -1;
  656. }
  657. }
  658. /* Call "predefined" reload here first */
  659. for (i = 0; reload_classes[i].name; i++) {
  660. if (!name || !strcasecmp(name, reload_classes[i].name)) {
  661. if (!reload_classes[i].reload_fn()) {
  662. ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", name);
  663. }
  664. res = 2; /* found and reloaded */
  665. }
  666. }
  667. if (name && res) {
  668. if (ast_opt_lock_confdir) {
  669. ast_unlock_path(ast_config_AST_CONFIG_DIR);
  670. }
  671. ast_mutex_unlock(&reloadlock);
  672. return res;
  673. }
  674. AST_LIST_LOCK(&module_list);
  675. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  676. const struct ast_module_info *info = cur->info;
  677. if (name && resource_name_match(name, cur->resource))
  678. continue;
  679. if (!cur->flags.running || cur->flags.declined) {
  680. if (!name)
  681. continue;
  682. ast_log(LOG_NOTICE, "The module '%s' was not properly initialized. "
  683. "Before reloading the module, you must run \"module load %s\" "
  684. "and fix whatever is preventing the module from being initialized.\n",
  685. name, name);
  686. res = 2; /* Don't report that the module was not found */
  687. break;
  688. }
  689. if (!info->reload) { /* cannot be reloaded */
  690. /* Nothing to reload, so reload is successful */
  691. ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
  692. if (res < 1) /* store result if possible */
  693. res = 1; /* 1 = no reload() method */
  694. continue;
  695. }
  696. res = 2;
  697. ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
  698. if (!info->reload()) {
  699. ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
  700. }
  701. }
  702. AST_LIST_UNLOCK(&module_list);
  703. if (ast_opt_lock_confdir) {
  704. ast_unlock_path(ast_config_AST_CONFIG_DIR);
  705. }
  706. ast_mutex_unlock(&reloadlock);
  707. return res;
  708. }
  709. static unsigned int inspect_module(const struct ast_module *mod)
  710. {
  711. if (!mod->info->description) {
  712. ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
  713. return 1;
  714. }
  715. if (!mod->info->key) {
  716. ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
  717. return 1;
  718. }
  719. if (verify_key((unsigned char *) mod->info->key)) {
  720. ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
  721. return 1;
  722. }
  723. if (!ast_strlen_zero(mod->info->buildopt_sum) &&
  724. strcmp(buildopt_sum, mod->info->buildopt_sum)) {
  725. ast_log(LOG_WARNING, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
  726. ast_log(LOG_WARNING, "Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
  727. return 1;
  728. }
  729. return 0;
  730. }
  731. static enum ast_module_load_result start_resource(struct ast_module *mod)
  732. {
  733. char tmp[256];
  734. enum ast_module_load_result res;
  735. if (mod->flags.running) {
  736. return AST_MODULE_LOAD_SUCCESS;
  737. }
  738. if (!mod->info->load) {
  739. return AST_MODULE_LOAD_FAILURE;
  740. }
  741. res = mod->info->load();
  742. switch (res) {
  743. case AST_MODULE_LOAD_SUCCESS:
  744. if (!ast_fully_booted) {
  745. ast_verb(1, "%s => (%s)\n", mod->resource, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
  746. if (ast_opt_console && !option_verbose) {
  747. /* This never looks good on anything but the root console, so
  748. * it's best not to try to funnel it through the logger. */
  749. fprintf(stdout, ".");
  750. }
  751. } else {
  752. ast_verb(1, "Loaded %s => (%s)\n", mod->resource, mod->info->description);
  753. }
  754. mod->flags.running = 1;
  755. ast_update_use_count();
  756. break;
  757. case AST_MODULE_LOAD_DECLINE:
  758. mod->flags.declined = 1;
  759. break;
  760. case AST_MODULE_LOAD_FAILURE:
  761. case AST_MODULE_LOAD_SKIP: /* modules should never return this value */
  762. case AST_MODULE_LOAD_PRIORITY:
  763. break;
  764. }
  765. return res;
  766. }
  767. /*! loads a resource based upon resource_name. If global_symbols_only is set
  768. * only modules with global symbols will be loaded.
  769. *
  770. * If the ast_heap is provided (not NULL) the module is found and added to the
  771. * heap without running the module's load() function. By doing this, modules
  772. * added to the resource_heap can be initialized later in order by priority.
  773. *
  774. * If the ast_heap is not provided, the module's load function will be executed
  775. * immediately */
  776. static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required)
  777. {
  778. struct ast_module *mod;
  779. enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;
  780. if ((mod = find_resource(resource_name, 0))) {
  781. if (mod->flags.running) {
  782. ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
  783. return AST_MODULE_LOAD_DECLINE;
  784. }
  785. if (global_symbols_only && !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS))
  786. return AST_MODULE_LOAD_SKIP;
  787. } else {
  788. #ifdef LOADABLE_MODULES
  789. if (!(mod = load_dynamic_module(resource_name, global_symbols_only, resource_heap))) {
  790. /* don't generate a warning message during load_modules() */
  791. if (!global_symbols_only) {
  792. ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
  793. return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
  794. } else {
  795. return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SKIP;
  796. }
  797. }
  798. #else
  799. ast_log(LOG_WARNING, "Module support is not available. Module '%s' could not be loaded.\n", resource_name);
  800. return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
  801. #endif
  802. }
  803. if (inspect_module(mod)) {
  804. ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
  805. #ifdef LOADABLE_MODULES
  806. unload_dynamic_module(mod);
  807. #endif
  808. return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
  809. }
  810. if (!mod->lib && mod->info->backup_globals && mod->info->backup_globals()) {
  811. ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
  812. return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
  813. }
  814. mod->flags.declined = 0;
  815. if (resource_heap) {
  816. ast_heap_push(resource_heap, mod);
  817. res = AST_MODULE_LOAD_PRIORITY;
  818. } else {
  819. res = start_resource(mod);
  820. }
  821. /* Now make sure that the list is sorted */
  822. AST_LIST_LOCK(&module_list);
  823. AST_LIST_REMOVE(&module_list, mod, entry);
  824. AST_LIST_INSERT_SORTALPHA(&module_list, mod, entry, resource);
  825. AST_LIST_UNLOCK(&module_list);
  826. return res;
  827. }
  828. int ast_load_resource(const char *resource_name)
  829. {
  830. int res;
  831. AST_LIST_LOCK(&module_list);
  832. res = load_resource(resource_name, 0, NULL, 0);
  833. if (!res) {
  834. ast_test_suite_event_notify("MODULE_LOAD", "Message: %s", resource_name);
  835. }
  836. AST_LIST_UNLOCK(&module_list);
  837. return res;
  838. }
  839. struct load_order_entry {
  840. char *resource;
  841. int required;
  842. AST_LIST_ENTRY(load_order_entry) entry;
  843. };
  844. AST_LIST_HEAD_NOLOCK(load_order, load_order_entry);
  845. static struct load_order_entry *add_to_load_order(const char *resource, struct load_order *load_order, int required)
  846. {
  847. struct load_order_entry *order;
  848. AST_LIST_TRAVERSE(load_order, order, entry) {
  849. if (!resource_name_match(order->resource, resource)) {
  850. /* Make sure we have the proper setting for the required field
  851. (we might have both load= and required= lines in modules.conf) */
  852. order->required |= required;
  853. return NULL;
  854. }
  855. }
  856. if (!(order = ast_calloc(1, sizeof(*order))))
  857. return NULL;
  858. order->resource = ast_strdup(resource);
  859. order->required = required;
  860. AST_LIST_INSERT_TAIL(load_order, order, entry);
  861. return order;
  862. }
  863. static int mod_load_cmp(void *a, void *b)
  864. {
  865. struct ast_module *a_mod = (struct ast_module *) a;
  866. struct ast_module *b_mod = (struct ast_module *) b;
  867. int res = -1;
  868. /* if load_pri is not set, default is 128. Lower is better*/
  869. unsigned char a_pri = ast_test_flag(a_mod->info, AST_MODFLAG_LOAD_ORDER) ? a_mod->info->load_pri : 128;
  870. unsigned char b_pri = ast_test_flag(b_mod->info, AST_MODFLAG_LOAD_ORDER) ? b_mod->info->load_pri : 128;
  871. if (a_pri == b_pri) {
  872. res = 0;
  873. } else if (a_pri < b_pri) {
  874. res = 1;
  875. }
  876. return res;
  877. }
  878. /*! loads modules in order by load_pri, updates mod_count
  879. \return -1 on failure to load module, -2 on failure to load required module, otherwise 0
  880. */
  881. static int load_resource_list(struct load_order *load_order, unsigned int global_symbols, int *mod_count)
  882. {
  883. struct ast_heap *resource_heap;
  884. struct load_order_entry *order;
  885. struct ast_module *mod;
  886. int count = 0;
  887. int res = 0;
  888. if(!(resource_heap = ast_heap_create(8, mod_load_cmp, -1))) {
  889. return -1;
  890. }
  891. /* first, add find and add modules to heap */
  892. AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
  893. switch (load_resource(order->resource, global_symbols, resource_heap, order->required)) {
  894. case AST_MODULE_LOAD_SUCCESS:
  895. case AST_MODULE_LOAD_DECLINE:
  896. AST_LIST_REMOVE_CURRENT(entry);
  897. ast_free(order->resource);
  898. ast_free(order);
  899. break;
  900. case AST_MODULE_LOAD_FAILURE:
  901. ast_log(LOG_ERROR, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
  902. fprintf(stderr, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
  903. res = order->required ? -2 : -1;
  904. goto done;
  905. case AST_MODULE_LOAD_SKIP:
  906. break;
  907. case AST_MODULE_LOAD_PRIORITY:
  908. AST_LIST_REMOVE_CURRENT(entry);
  909. ast_free(order->resource);
  910. ast_free(order);
  911. break;
  912. }
  913. }
  914. AST_LIST_TRAVERSE_SAFE_END;
  915. /* second remove modules from heap sorted by priority */
  916. while ((mod = ast_heap_pop(resource_heap))) {
  917. switch (start_resource(mod)) {
  918. case AST_MODULE_LOAD_SUCCESS:
  919. count++;
  920. case AST_MODULE_LOAD_DECLINE:
  921. break;
  922. case AST_MODULE_LOAD_FAILURE:
  923. res = -1;
  924. goto done;
  925. case AST_MODULE_LOAD_SKIP:
  926. case AST_MODULE_LOAD_PRIORITY:
  927. break;
  928. }
  929. }
  930. done:
  931. if (mod_count) {
  932. *mod_count += count;
  933. }
  934. ast_heap_destroy(resource_heap);
  935. return res;
  936. }
  937. int load_modules(unsigned int preload_only)
  938. {
  939. struct ast_config *cfg;
  940. struct ast_module *mod;
  941. struct load_order_entry *order;
  942. struct ast_variable *v;
  943. unsigned int load_count;
  944. struct load_order load_order;
  945. int res = 0;
  946. struct ast_flags config_flags = { 0 };
  947. int modulecount = 0;
  948. #ifdef LOADABLE_MODULES
  949. struct dirent *dirent;
  950. DIR *dir;
  951. #endif
  952. /* all embedded modules have registered themselves by now */
  953. embedding = 0;
  954. ast_verb(1, "Asterisk Dynamic Loader Starting:\n");
  955. AST_LIST_HEAD_INIT_NOLOCK(&load_order);
  956. AST_LIST_LOCK(&module_list);
  957. if (embedded_module_list.first) {
  958. module_list.first = embedded_module_list.first;
  959. module_list.last = embedded_module_list.last;
  960. embedded_module_list.first = NULL;
  961. }
  962. cfg = ast_config_load2(AST_MODULE_CONFIG, "" /* core, can't reload */, config_flags);
  963. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  964. ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
  965. goto done;
  966. }
  967. /* first, find all the modules we have been explicitly requested to load */
  968. for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
  969. if (!strcasecmp(v->name, preload_only ? "preload" : "load")) {
  970. add_to_load_order(v->value, &load_order, 0);
  971. }
  972. if (!strcasecmp(v->name, preload_only ? "preload-require" : "require")) {
  973. /* Add the module to the list and make sure it's required */
  974. add_to_load_order(v->value, &load_order, 1);
  975. ast_debug(2, "Adding module to required list: %s (%s)\n", v->value, v->name);
  976. }
  977. }
  978. /* check if 'autoload' is on */
  979. if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
  980. /* if so, first add all the embedded modules that are not already running to the load order */
  981. AST_LIST_TRAVERSE(&module_list, mod, entry) {
  982. /* if it's not embedded, skip it */
  983. if (mod->lib)
  984. continue;
  985. if (mod->flags.running)
  986. continue;
  987. add_to_load_order(mod->resource, &load_order, 0);
  988. }
  989. #ifdef LOADABLE_MODULES
  990. /* if we are allowed to load dynamic modules, scan the directory for
  991. for all available modules and add them as well */
  992. if ((dir = opendir(ast_config_AST_MODULE_DIR))) {
  993. while ((dirent = readdir(dir))) {
  994. int ld = strlen(dirent->d_name);
  995. /* Must end in .so to load it. */
  996. if (ld < 4)
  997. continue;
  998. if (strcasecmp(dirent->d_name + ld - 3, ".so"))
  999. continue;
  1000. /* if there is already a module by this name in the module_list,
  1001. skip this file */
  1002. if (find_resource(dirent->d_name, 0))
  1003. continue;
  1004. add_to_load_order(dirent->d_name, &load_order, 0);
  1005. }
  1006. closedir(dir);
  1007. } else {
  1008. if (!ast_opt_quiet)
  1009. ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
  1010. ast_config_AST_MODULE_DIR);
  1011. }
  1012. #endif
  1013. }
  1014. /* now scan the config for any modules we are prohibited from loading and
  1015. remove them from the load order */
  1016. for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
  1017. if (strcasecmp(v->name, "noload"))
  1018. continue;
  1019. AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
  1020. if (!resource_name_match(order->resource, v->value)) {
  1021. AST_LIST_REMOVE_CURRENT(entry);
  1022. ast_free(order->resource);
  1023. ast_free(order);
  1024. }
  1025. }
  1026. AST_LIST_TRAVERSE_SAFE_END;
  1027. }
  1028. /* we are done with the config now, all the information we need is in the
  1029. load_order list */
  1030. ast_config_destroy(cfg);
  1031. load_count = 0;
  1032. AST_LIST_TRAVERSE(&load_order, order, entry)
  1033. load_count++;
  1034. if (load_count)
  1035. ast_log(LOG_NOTICE, "%u modules will be loaded.\n", load_count);
  1036. /* first, load only modules that provide global symbols */
  1037. if ((res = load_resource_list(&load_order, 1, &modulecount)) < 0) {
  1038. goto done;
  1039. }
  1040. /* now load everything else */
  1041. if ((res = load_resource_list(&load_order, 0, &modulecount)) < 0) {
  1042. goto done;
  1043. }
  1044. done:
  1045. while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
  1046. ast_free(order->resource);
  1047. ast_free(order);
  1048. }
  1049. AST_LIST_UNLOCK(&module_list);
  1050. /* Tell manager clients that are aggressive at logging in that we're done
  1051. loading modules. If there's a DNS problem in chan_sip, we might not
  1052. even reach this */
  1053. /*** DOCUMENTATION
  1054. <managerEventInstance>
  1055. <synopsis>Raised when all dynamic modules have finished their initial loading.</synopsis>
  1056. <syntax>
  1057. <parameter name="ModuleSelection">
  1058. <enumlist>
  1059. <enum name="Preload"/>
  1060. <enum name="All"/>
  1061. </enumlist>
  1062. </parameter>
  1063. </syntax>
  1064. </managerEventInstance>
  1065. ***/
  1066. manager_event(EVENT_FLAG_SYSTEM, "ModuleLoadReport", "ModuleLoadStatus: Done\r\nModuleSelection: %s\r\nModuleCount: %d\r\n", preload_only ? "Preload" : "All", modulecount);
  1067. return res;
  1068. }
  1069. void ast_update_use_count(void)
  1070. {
  1071. /* Notify any module monitors that the use count for a
  1072. resource has changed */
  1073. struct loadupdate *m;
  1074. AST_LIST_LOCK(&updaters);
  1075. AST_LIST_TRAVERSE(&updaters, m, entry)
  1076. m->updater();
  1077. AST_LIST_UNLOCK(&updaters);
  1078. }
  1079. int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
  1080. const char *like)
  1081. {
  1082. struct ast_module *cur;
  1083. int unlock = -1;
  1084. int total_mod_loaded = 0;
  1085. if (AST_LIST_TRYLOCK(&module_list))
  1086. unlock = 0;
  1087. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  1088. total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
  1089. }
  1090. if (unlock)
  1091. AST_LIST_UNLOCK(&module_list);
  1092. return total_mod_loaded;
  1093. }
  1094. /*! \brief Check if module exists */
  1095. int ast_module_check(const char *name)
  1096. {
  1097. struct ast_module *cur;
  1098. if (ast_strlen_zero(name))
  1099. return 0; /* FALSE */
  1100. cur = find_resource(name, 1);
  1101. return (cur != NULL);
  1102. }
  1103. int ast_loader_register(int (*v)(void))
  1104. {
  1105. struct loadupdate *tmp;
  1106. if (!(tmp = ast_malloc(sizeof(*tmp))))
  1107. return -1;
  1108. tmp->updater = v;
  1109. AST_LIST_LOCK(&updaters);
  1110. AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
  1111. AST_LIST_UNLOCK(&updaters);
  1112. return 0;
  1113. }
  1114. int ast_loader_unregister(int (*v)(void))
  1115. {
  1116. struct loadupdate *cur;
  1117. AST_LIST_LOCK(&updaters);
  1118. AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
  1119. if (cur->updater == v) {
  1120. AST_LIST_REMOVE_CURRENT(entry);
  1121. break;
  1122. }
  1123. }
  1124. AST_LIST_TRAVERSE_SAFE_END;
  1125. AST_LIST_UNLOCK(&updaters);
  1126. return cur ? 0 : -1;
  1127. }
  1128. struct ast_module *__ast_module_ref(struct ast_module *mod, const char *file, int line, const char *func)
  1129. {
  1130. if (!mod) {
  1131. return NULL;
  1132. }
  1133. #ifdef REF_DEBUG
  1134. __ao2_ref_debug(mod->ref_debug, +1, "", file, line, func);
  1135. #endif
  1136. ast_atomic_fetchadd_int(&mod->usecount, +1);
  1137. ast_update_use_count();
  1138. return mod;
  1139. }
  1140. void __ast_module_shutdown_ref(struct ast_module *mod, const char *file, int line, const char *func)
  1141. {
  1142. if (!mod || mod->flags.keepuntilshutdown) {
  1143. return;
  1144. }
  1145. __ast_module_ref(mod, file, line, func);
  1146. mod->flags.keepuntilshutdown = 1;
  1147. }
  1148. void __ast_module_unref(struct ast_module *mod, const char *file, int line, const char *func)
  1149. {
  1150. if (!mod) {
  1151. return;
  1152. }
  1153. #ifdef REF_DEBUG
  1154. __ao2_ref_debug(mod->ref_debug, -1, "", file, line, func);
  1155. #endif
  1156. ast_atomic_fetchadd_int(&mod->usecount, -1);
  1157. ast_update_use_count();
  1158. }
  1159. /* The following exists for ABI compatibility */
  1160. #undef ast_module_ref
  1161. #undef ast_module_unref
  1162. struct ast_module *ast_module_ref(struct ast_module *mod);
  1163. void ast_module_unref(struct ast_module *mod);
  1164. struct ast_module *ast_module_ref(struct ast_module *mod)
  1165. {
  1166. return __ast_module_ref(mod, __FILE__, __LINE__, __PRETTY_FUNCTION__);
  1167. }
  1168. void ast_module_unref(struct ast_module *mod)
  1169. {
  1170. __ast_module_unref(mod, __FILE__, __LINE__, __PRETTY_FUNCTION__);
  1171. }