loader.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/_private.h"
  31. #include "asterisk/paths.h" /* use ast_config_AST_MODULE_DIR */
  32. #include <dirent.h>
  33. #include "asterisk/linkedlists.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/config.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/term.h"
  38. #include "asterisk/manager.h"
  39. #include "asterisk/cdr.h"
  40. #include "asterisk/enum.h"
  41. #include "asterisk/rtp.h"
  42. #include "asterisk/http.h"
  43. #include "asterisk/lock.h"
  44. #include "asterisk/features.h"
  45. #include <dlfcn.h>
  46. #include "asterisk/md5.h"
  47. #include "asterisk/utils.h"
  48. #ifndef RTLD_NOW
  49. #define RTLD_NOW 0
  50. #endif
  51. #ifndef RTLD_LOCAL
  52. #define RTLD_LOCAL 0
  53. #endif
  54. struct ast_module_user {
  55. struct ast_channel *chan;
  56. AST_LIST_ENTRY(ast_module_user) entry;
  57. };
  58. AST_LIST_HEAD(module_user_list, ast_module_user);
  59. static unsigned char expected_key[] =
  60. { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
  61. 0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
  62. static char buildopt_sum[33] = AST_BUILDOPT_SUM;
  63. static unsigned int embedding = 1; /* we always start out by registering embedded modules,
  64. since they are here before we dlopen() any
  65. */
  66. struct ast_module {
  67. const struct ast_module_info *info;
  68. void *lib; /* the shared lib, or NULL if embedded */
  69. int usecount; /* the number of 'users' currently in this module */
  70. struct module_user_list users; /* the list of users in the module */
  71. struct {
  72. unsigned int running:1;
  73. unsigned int declined:1;
  74. } flags;
  75. AST_LIST_ENTRY(ast_module) entry;
  76. char resource[0];
  77. };
  78. static AST_LIST_HEAD_STATIC(module_list, ast_module);
  79. /*
  80. * module_list is cleared by its constructor possibly after
  81. * we start accumulating embedded modules, so we need to
  82. * use another list (without the lock) to accumulate them.
  83. * Then we update the main list when embedding is done.
  84. */
  85. static struct module_list embedded_module_list;
  86. struct loadupdate {
  87. int (*updater)(void);
  88. AST_LIST_ENTRY(loadupdate) entry;
  89. };
  90. static AST_LIST_HEAD_STATIC(updaters, loadupdate);
  91. AST_MUTEX_DEFINE_STATIC(reloadlock);
  92. struct reload_queue_item {
  93. AST_LIST_ENTRY(reload_queue_item) entry;
  94. char module[0];
  95. };
  96. static int do_full_reload = 0;
  97. static AST_LIST_HEAD_STATIC(reload_queue, reload_queue_item);
  98. /* when dynamic modules are being loaded, ast_module_register() will
  99. need to know what filename the module was loaded from while it
  100. is being registered
  101. */
  102. struct ast_module *resource_being_loaded;
  103. /*! \brief Load modules in this order. */
  104. enum module_load_pass {
  105. /*! \brief AST_MODFLAG_LOAD_FIRST */
  106. LOAD_FIRST,
  107. /*! \brief AST_MODFLAG_GLOBAL_SYMBOLS */
  108. LOAD_GLOBAL_SYMBOLS,
  109. /*! \brief everything that is left */
  110. LOAD_ALL,
  111. /*! \brief Must remain at the end. */
  112. LOAD_DONE,
  113. };
  114. /* XXX: should we check for duplicate resource names here? */
  115. void ast_module_register(const struct ast_module_info *info)
  116. {
  117. struct ast_module *mod;
  118. if (embedding) {
  119. if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
  120. return;
  121. strcpy(mod->resource, info->name);
  122. } else {
  123. mod = resource_being_loaded;
  124. }
  125. mod->info = info;
  126. AST_LIST_HEAD_INIT(&mod->users);
  127. /* during startup, before the loader has been initialized,
  128. there are no threads, so there is no need to take the lock
  129. on this list to manipulate it. it is also possible that it
  130. might be unsafe to use the list lock at that point... so
  131. let's avoid it altogether
  132. */
  133. if (embedding) {
  134. AST_LIST_INSERT_TAIL(&embedded_module_list, mod, entry);
  135. } else {
  136. AST_LIST_LOCK(&module_list);
  137. /* it is paramount that the new entry be placed at the tail of
  138. the list, otherwise the code that uses dlopen() to load
  139. dynamic modules won't be able to find out if the module it
  140. just opened was registered or failed to load
  141. */
  142. AST_LIST_INSERT_TAIL(&module_list, mod, entry);
  143. AST_LIST_UNLOCK(&module_list);
  144. }
  145. /* give the module a copy of its own handle, for later use in registrations and the like */
  146. *((struct ast_module **) &(info->self)) = mod;
  147. }
  148. void ast_module_unregister(const struct ast_module_info *info)
  149. {
  150. struct ast_module *mod = NULL;
  151. /* it is assumed that the users list in the module structure
  152. will already be empty, or we cannot have gotten to this
  153. point
  154. */
  155. AST_LIST_LOCK(&module_list);
  156. AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
  157. if (mod->info == info) {
  158. AST_LIST_REMOVE_CURRENT(entry);
  159. break;
  160. }
  161. }
  162. AST_LIST_TRAVERSE_SAFE_END;
  163. AST_LIST_UNLOCK(&module_list);
  164. if (mod) {
  165. AST_LIST_HEAD_DESTROY(&mod->users);
  166. ast_free(mod);
  167. }
  168. }
  169. struct ast_module_user *__ast_module_user_add(struct ast_module *mod,
  170. struct ast_channel *chan)
  171. {
  172. struct ast_module_user *u = ast_calloc(1, sizeof(*u));
  173. if (!u)
  174. return NULL;
  175. u->chan = chan;
  176. AST_LIST_LOCK(&mod->users);
  177. AST_LIST_INSERT_HEAD(&mod->users, u, entry);
  178. AST_LIST_UNLOCK(&mod->users);
  179. ast_atomic_fetchadd_int(&mod->usecount, +1);
  180. ast_update_use_count();
  181. return u;
  182. }
  183. void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
  184. {
  185. AST_LIST_LOCK(&mod->users);
  186. AST_LIST_REMOVE(&mod->users, u, entry);
  187. AST_LIST_UNLOCK(&mod->users);
  188. ast_atomic_fetchadd_int(&mod->usecount, -1);
  189. ast_free(u);
  190. ast_update_use_count();
  191. }
  192. void __ast_module_user_hangup_all(struct ast_module *mod)
  193. {
  194. struct ast_module_user *u;
  195. AST_LIST_LOCK(&mod->users);
  196. while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
  197. ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
  198. ast_atomic_fetchadd_int(&mod->usecount, -1);
  199. ast_free(u);
  200. }
  201. AST_LIST_UNLOCK(&mod->users);
  202. ast_update_use_count();
  203. }
  204. /*! \note
  205. * In addition to modules, the reload command handles some extra keywords
  206. * which are listed here together with the corresponding handlers.
  207. * This table is also used by the command completion code.
  208. */
  209. static struct reload_classes {
  210. const char *name;
  211. int (*reload_fn)(void);
  212. } reload_classes[] = { /* list in alpha order, longest match first for cli completion */
  213. { "cdr", ast_cdr_engine_reload },
  214. { "dnsmgr", dnsmgr_reload },
  215. { "extconfig", read_config_maps },
  216. { "enum", ast_enum_reload },
  217. { "manager", reload_manager },
  218. { "rtp", ast_rtp_reload },
  219. { "http", ast_http_reload },
  220. { "logger", logger_reload },
  221. { "features", ast_features_reload },
  222. { NULL, NULL }
  223. };
  224. static int printdigest(const unsigned char *d)
  225. {
  226. int x, pos;
  227. char buf[256]; /* large enough so we don't have to worry */
  228. for (pos = 0, x = 0; x < 16; x++)
  229. pos += sprintf(buf + pos, " %02x", *d++);
  230. ast_debug(1, "Unexpected signature:%s\n", buf);
  231. return 0;
  232. }
  233. static int key_matches(const unsigned char *key1, const unsigned char *key2)
  234. {
  235. int x;
  236. for (x = 0; x < 16; x++) {
  237. if (key1[x] != key2[x])
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. static int verify_key(const unsigned char *key)
  243. {
  244. struct MD5Context c;
  245. unsigned char digest[16];
  246. MD5Init(&c);
  247. MD5Update(&c, key, strlen((char *)key));
  248. MD5Final(digest, &c);
  249. if (key_matches(expected_key, digest))
  250. return 0;
  251. printdigest(digest);
  252. return -1;
  253. }
  254. static int resource_name_match(const char *name1_in, const char *name2_in)
  255. {
  256. char *name1 = (char *) name1_in;
  257. char *name2 = (char *) name2_in;
  258. /* trim off any .so extensions */
  259. if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
  260. name1 = ast_strdupa(name1);
  261. name1[strlen(name1) - 3] = '\0';
  262. }
  263. if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
  264. name2 = ast_strdupa(name2);
  265. name2[strlen(name2) - 3] = '\0';
  266. }
  267. return strcasecmp(name1, name2);
  268. }
  269. static struct ast_module *find_resource(const char *resource, int do_lock)
  270. {
  271. struct ast_module *cur;
  272. if (do_lock)
  273. AST_LIST_LOCK(&module_list);
  274. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  275. if (!resource_name_match(resource, cur->resource))
  276. break;
  277. }
  278. if (do_lock)
  279. AST_LIST_UNLOCK(&module_list);
  280. return cur;
  281. }
  282. #ifdef LOADABLE_MODULES
  283. static void unload_dynamic_module(struct ast_module *mod)
  284. {
  285. void *lib = mod->lib;
  286. /* WARNING: the structure pointed to by mod is going to
  287. disappear when this operation succeeds, so we can't
  288. dereference it */
  289. if (lib)
  290. while (!dlclose(lib));
  291. }
  292. static struct ast_module *load_dynamic_module(const char *resource_in, enum module_load_pass load_pass)
  293. {
  294. char fn[PATH_MAX] = "";
  295. void *lib = NULL;
  296. struct ast_module *mod;
  297. unsigned int wants_global = 0, not_yet = 0;
  298. int space; /* room needed for the descriptor */
  299. int missing_so = 0;
  300. space = sizeof(*resource_being_loaded) + strlen(resource_in) + 1;
  301. if (strcasecmp(resource_in + strlen(resource_in) - 3, ".so")) {
  302. missing_so = 1;
  303. space += 3; /* room for the extra ".so" */
  304. }
  305. snprintf(fn, sizeof(fn), "%s/%s%s", ast_config_AST_MODULE_DIR, resource_in, missing_so ? ".so" : "");
  306. /* make a first load of the module in 'quiet' mode... don't try to resolve
  307. any symbols, and don't export any symbols. this will allow us to peek into
  308. the module's info block (if available) to see what flags it has set */
  309. resource_being_loaded = ast_calloc(1, space);
  310. if (!resource_being_loaded)
  311. return NULL;
  312. strcpy(resource_being_loaded->resource, resource_in);
  313. if (missing_so)
  314. strcat(resource_being_loaded->resource, ".so");
  315. if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_LOCAL))) {
  316. ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
  317. ast_free(resource_being_loaded);
  318. return NULL;
  319. }
  320. /* the dlopen() succeeded, let's find out if the module
  321. registered itself */
  322. /* note that this will only work properly as long as
  323. ast_module_register() (which is called by the module's
  324. constructor) places the new module at the tail of the
  325. module_list
  326. */
  327. if (resource_being_loaded != (mod = AST_LIST_LAST(&module_list))) {
  328. ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
  329. /* no, it did not, so close it and return */
  330. while (!dlclose(lib));
  331. /* note that the module's destructor will call ast_module_unregister(),
  332. which will free the structure we allocated in resource_being_loaded */
  333. return NULL;
  334. }
  335. switch (load_pass) {
  336. case LOAD_FIRST:
  337. not_yet = !ast_test_flag(mod->info, AST_MODFLAG_LOAD_FIRST);
  338. break;
  339. case LOAD_GLOBAL_SYMBOLS:
  340. wants_global = ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS);
  341. not_yet = !wants_global;
  342. break;
  343. case LOAD_ALL:
  344. break;
  345. case LOAD_DONE:
  346. ast_log(LOG_ERROR, "Satan just bought a snowblower! (This should never happen, btw.)\n");
  347. break;
  348. }
  349. if (not_yet) {
  350. while (!dlclose(lib));
  351. return NULL;
  352. }
  353. while (!dlclose(lib));
  354. resource_being_loaded = NULL;
  355. /* start the load process again */
  356. resource_being_loaded = ast_calloc(1, space);
  357. if (!resource_being_loaded)
  358. return NULL;
  359. strcpy(resource_being_loaded->resource, resource_in);
  360. if (missing_so)
  361. strcat(resource_being_loaded->resource, ".so");
  362. if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
  363. ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
  364. ast_free(resource_being_loaded);
  365. return NULL;
  366. }
  367. /* since the module was successfully opened, and it registered itself
  368. the previous time we did that, we're going to assume it worked this
  369. time too :) */
  370. AST_LIST_LAST(&module_list)->lib = lib;
  371. resource_being_loaded = NULL;
  372. return AST_LIST_LAST(&module_list);
  373. }
  374. #endif
  375. void ast_module_shutdown(void)
  376. {
  377. struct ast_module *mod;
  378. int somethingchanged = 1, final = 0;
  379. AST_LIST_LOCK(&module_list);
  380. /*!\note Some resources, like timers, are started up dynamically, and thus
  381. * may be still in use, even if all channels are dead. We must therefore
  382. * check the usecount before asking modules to unload. */
  383. do {
  384. if (!somethingchanged) {
  385. /*!\note If we go through the entire list without changing
  386. * anything, ignore the usecounts and unload, then exit. */
  387. final = 1;
  388. }
  389. /* Reset flag before traversing the list */
  390. somethingchanged = 0;
  391. AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
  392. if (!final && mod->usecount) {
  393. continue;
  394. }
  395. AST_LIST_REMOVE_CURRENT(entry);
  396. if (mod->info->unload) {
  397. mod->info->unload();
  398. }
  399. AST_LIST_HEAD_DESTROY(&mod->users);
  400. free(mod);
  401. somethingchanged = 1;
  402. }
  403. AST_LIST_TRAVERSE_SAFE_END;
  404. } while (somethingchanged && !final);
  405. AST_LIST_UNLOCK(&module_list);
  406. }
  407. int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
  408. {
  409. struct ast_module *mod;
  410. int res = -1;
  411. int error = 0;
  412. AST_LIST_LOCK(&module_list);
  413. if (!(mod = find_resource(resource_name, 0))) {
  414. AST_LIST_UNLOCK(&module_list);
  415. ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
  416. return -1;
  417. }
  418. if (!(mod->flags.running || mod->flags.declined))
  419. error = 1;
  420. if (!error && (mod->usecount > 0)) {
  421. if (force)
  422. ast_log(LOG_WARNING, "Warning: Forcing removal of module '%s' with use count %d\n",
  423. resource_name, mod->usecount);
  424. else {
  425. ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
  426. mod->usecount);
  427. error = 1;
  428. }
  429. }
  430. if (!error) {
  431. __ast_module_user_hangup_all(mod);
  432. res = mod->info->unload();
  433. if (res) {
  434. ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
  435. if (force <= AST_FORCE_FIRM)
  436. error = 1;
  437. else
  438. ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
  439. }
  440. }
  441. if (!error)
  442. mod->flags.running = mod->flags.declined = 0;
  443. AST_LIST_UNLOCK(&module_list);
  444. if (!error && !mod->lib && mod->info && mod->info->restore_globals)
  445. mod->info->restore_globals();
  446. #ifdef LOADABLE_MODULES
  447. if (!error)
  448. unload_dynamic_module(mod);
  449. #endif
  450. if (!error)
  451. ast_update_use_count();
  452. return res;
  453. }
  454. char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload)
  455. {
  456. struct ast_module *cur;
  457. int i, which=0, l = strlen(word);
  458. char *ret = NULL;
  459. if (pos != rpos)
  460. return NULL;
  461. AST_LIST_LOCK(&module_list);
  462. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  463. if (!strncasecmp(word, cur->resource, l) &&
  464. (cur->info->reload || !needsreload) &&
  465. ++which > state) {
  466. ret = ast_strdup(cur->resource);
  467. break;
  468. }
  469. }
  470. AST_LIST_UNLOCK(&module_list);
  471. if (!ret) {
  472. for (i=0; !ret && reload_classes[i].name; i++) {
  473. if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
  474. ret = ast_strdup(reload_classes[i].name);
  475. }
  476. }
  477. return ret;
  478. }
  479. void ast_process_pending_reloads(void)
  480. {
  481. struct reload_queue_item *item;
  482. if (!ast_fully_booted) {
  483. return;
  484. }
  485. AST_LIST_LOCK(&reload_queue);
  486. if (do_full_reload) {
  487. do_full_reload = 0;
  488. AST_LIST_UNLOCK(&reload_queue);
  489. ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
  490. ast_module_reload(NULL);
  491. return;
  492. }
  493. while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
  494. ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
  495. ast_module_reload(item->module);
  496. ast_free(item);
  497. }
  498. AST_LIST_UNLOCK(&reload_queue);
  499. }
  500. static void queue_reload_request(const char *module)
  501. {
  502. struct reload_queue_item *item;
  503. AST_LIST_LOCK(&reload_queue);
  504. if (do_full_reload) {
  505. AST_LIST_UNLOCK(&reload_queue);
  506. return;
  507. }
  508. if (ast_strlen_zero(module)) {
  509. /* A full reload request (when module is NULL) wipes out any previous
  510. reload requests and causes the queue to ignore future ones */
  511. while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
  512. ast_free(item);
  513. }
  514. do_full_reload = 1;
  515. } else {
  516. /* No reason to add the same module twice */
  517. AST_LIST_TRAVERSE(&reload_queue, item, entry) {
  518. if (!strcasecmp(item->module, module)) {
  519. AST_LIST_UNLOCK(&reload_queue);
  520. return;
  521. }
  522. }
  523. item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
  524. if (!item) {
  525. ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
  526. AST_LIST_UNLOCK(&reload_queue);
  527. return;
  528. }
  529. strcpy(item->module, module);
  530. AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
  531. }
  532. AST_LIST_UNLOCK(&reload_queue);
  533. }
  534. int ast_module_reload(const char *name)
  535. {
  536. struct ast_module *cur;
  537. int res = 0; /* return value. 0 = not found, others, see below */
  538. int i;
  539. /* If we aren't fully booted, we just pretend we reloaded but we queue this
  540. up to run once we are booted up. */
  541. if (!ast_fully_booted) {
  542. queue_reload_request(name);
  543. return 0;
  544. }
  545. if (ast_mutex_trylock(&reloadlock)) {
  546. ast_verbose("The previous reload command didn't finish yet\n");
  547. return -1; /* reload already in progress */
  548. }
  549. ast_lastreloadtime = ast_tvnow();
  550. /* Call "predefined" reload here first */
  551. for (i = 0; reload_classes[i].name; i++) {
  552. if (!name || !strcasecmp(name, reload_classes[i].name)) {
  553. reload_classes[i].reload_fn(); /* XXX should check error ? */
  554. res = 2; /* found and reloaded */
  555. }
  556. }
  557. if (name && res) {
  558. ast_mutex_unlock(&reloadlock);
  559. return res;
  560. }
  561. AST_LIST_LOCK(&module_list);
  562. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  563. const struct ast_module_info *info = cur->info;
  564. if (name && resource_name_match(name, cur->resource))
  565. continue;
  566. if (!cur->flags.running || cur->flags.declined) {
  567. if (!name)
  568. continue;
  569. ast_log(LOG_NOTICE, "The module '%s' was not properly initialized. "
  570. "Before reloading the module, you must run \"module load %s\" "
  571. "and fix whatever is preventing the module from being initialized.\n",
  572. name, name);
  573. res = 2; /* Don't report that the module was not found */
  574. break;
  575. }
  576. if (!info->reload) { /* cannot be reloaded */
  577. if (res < 1) /* store result if possible */
  578. res = 1; /* 1 = no reload() method */
  579. continue;
  580. }
  581. res = 2;
  582. ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
  583. info->reload();
  584. }
  585. AST_LIST_UNLOCK(&module_list);
  586. ast_mutex_unlock(&reloadlock);
  587. return res;
  588. }
  589. static unsigned int inspect_module(const struct ast_module *mod)
  590. {
  591. if (!mod->info->description) {
  592. ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
  593. return 1;
  594. }
  595. if (!mod->info->key) {
  596. ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
  597. return 1;
  598. }
  599. if (verify_key((unsigned char *) mod->info->key)) {
  600. ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
  601. return 1;
  602. }
  603. if (!ast_strlen_zero(mod->info->buildopt_sum) &&
  604. strcmp(buildopt_sum, mod->info->buildopt_sum)) {
  605. ast_log(LOG_WARNING, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
  606. ast_log(LOG_WARNING, "Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
  607. return 1;
  608. }
  609. return 0;
  610. }
  611. static enum ast_module_load_result load_resource(const char *resource_name, enum module_load_pass load_pass)
  612. {
  613. struct ast_module *mod;
  614. enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;
  615. char tmp[256];
  616. if ((mod = find_resource(resource_name, 0))) {
  617. if (mod->flags.running) {
  618. ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
  619. return AST_MODULE_LOAD_DECLINE;
  620. }
  621. switch (load_pass) {
  622. case LOAD_FIRST:
  623. if (!ast_test_flag(mod->info, AST_MODFLAG_LOAD_FIRST)) {
  624. return AST_MODULE_LOAD_SKIP;
  625. }
  626. break;
  627. case LOAD_GLOBAL_SYMBOLS:
  628. if (!ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS)) {
  629. return AST_MODULE_LOAD_SKIP;
  630. }
  631. break;
  632. case LOAD_ALL:
  633. break;
  634. case LOAD_DONE:
  635. ast_log(LOG_ERROR, "This should never happen, -EFLAMES!\n");
  636. break;
  637. }
  638. } else {
  639. #ifdef LOADABLE_MODULES
  640. if (!(mod = load_dynamic_module(resource_name, load_pass))) {
  641. /* don't generate a warning message during load_modules() */
  642. if (load_pass == LOAD_ALL) {
  643. ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
  644. return AST_MODULE_LOAD_DECLINE;
  645. } else {
  646. return AST_MODULE_LOAD_SKIP;
  647. }
  648. }
  649. #else
  650. ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
  651. return AST_MODULE_LOAD_DECLINE;
  652. #endif
  653. }
  654. if (inspect_module(mod)) {
  655. ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
  656. #ifdef LOADABLE_MODULES
  657. unload_dynamic_module(mod);
  658. #endif
  659. return AST_MODULE_LOAD_DECLINE;
  660. }
  661. if (!mod->lib && mod->info->backup_globals()) {
  662. ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
  663. return AST_MODULE_LOAD_DECLINE;
  664. }
  665. mod->flags.declined = 0;
  666. if (mod->info->load)
  667. res = mod->info->load();
  668. switch (res) {
  669. case AST_MODULE_LOAD_SUCCESS:
  670. if (!ast_fully_booted) {
  671. ast_verb(1, "%s => (%s)\n", resource_name, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
  672. if (ast_opt_console && !option_verbose)
  673. ast_verbose( ".");
  674. } else {
  675. ast_verb(1, "Loaded %s => (%s)\n", resource_name, mod->info->description);
  676. }
  677. mod->flags.running = 1;
  678. ast_update_use_count();
  679. break;
  680. case AST_MODULE_LOAD_DECLINE:
  681. mod->flags.declined = 1;
  682. break;
  683. case AST_MODULE_LOAD_FAILURE:
  684. break;
  685. case AST_MODULE_LOAD_SKIP:
  686. /* modules should never return this value */
  687. break;
  688. }
  689. return res;
  690. }
  691. int ast_load_resource(const char *resource_name)
  692. {
  693. int res;
  694. AST_LIST_LOCK(&module_list);
  695. res = load_resource(resource_name, LOAD_ALL);
  696. AST_LIST_UNLOCK(&module_list);
  697. return res;
  698. }
  699. struct load_order_entry {
  700. char *resource;
  701. AST_LIST_ENTRY(load_order_entry) entry;
  702. };
  703. AST_LIST_HEAD_NOLOCK(load_order, load_order_entry);
  704. static struct load_order_entry *add_to_load_order(const char *resource, struct load_order *load_order)
  705. {
  706. struct load_order_entry *order;
  707. AST_LIST_TRAVERSE(load_order, order, entry) {
  708. if (!resource_name_match(order->resource, resource))
  709. return NULL;
  710. }
  711. if (!(order = ast_calloc(1, sizeof(*order))))
  712. return NULL;
  713. order->resource = ast_strdup(resource);
  714. AST_LIST_INSERT_TAIL(load_order, order, entry);
  715. return order;
  716. }
  717. int load_modules(unsigned int preload_only)
  718. {
  719. struct ast_config *cfg;
  720. struct ast_module *mod;
  721. struct load_order_entry *order;
  722. struct ast_variable *v;
  723. unsigned int load_count;
  724. struct load_order load_order;
  725. int res = 0;
  726. struct ast_flags config_flags = { 0 };
  727. int modulecount = 0;
  728. int load_pass;
  729. #ifdef LOADABLE_MODULES
  730. struct dirent *dirent;
  731. DIR *dir;
  732. #endif
  733. /* all embedded modules have registered themselves by now */
  734. embedding = 0;
  735. ast_verb(1, "Asterisk Dynamic Loader Starting:\n");
  736. AST_LIST_HEAD_INIT_NOLOCK(&load_order);
  737. AST_LIST_LOCK(&module_list);
  738. if (embedded_module_list.first) {
  739. module_list.first = embedded_module_list.first;
  740. module_list.last = embedded_module_list.last;
  741. embedded_module_list.first = NULL;
  742. }
  743. if (!(cfg = ast_config_load(AST_MODULE_CONFIG, config_flags))) {
  744. ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
  745. goto done;
  746. }
  747. /* first, find all the modules we have been explicitly requested to load */
  748. for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
  749. if (!strcasecmp(v->name, preload_only ? "preload" : "load")) {
  750. add_to_load_order(v->value, &load_order);
  751. }
  752. }
  753. /* check if 'autoload' is on */
  754. if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
  755. /* if so, first add all the embedded modules that are not already running to the load order */
  756. AST_LIST_TRAVERSE(&module_list, mod, entry) {
  757. /* if it's not embedded, skip it */
  758. if (mod->lib)
  759. continue;
  760. if (mod->flags.running)
  761. continue;
  762. order = add_to_load_order(mod->resource, &load_order);
  763. }
  764. #ifdef LOADABLE_MODULES
  765. /* if we are allowed to load dynamic modules, scan the directory for
  766. for all available modules and add them as well */
  767. if ((dir = opendir(ast_config_AST_MODULE_DIR))) {
  768. while ((dirent = readdir(dir))) {
  769. int ld = strlen(dirent->d_name);
  770. /* Must end in .so to load it. */
  771. if (ld < 4)
  772. continue;
  773. if (strcasecmp(dirent->d_name + ld - 3, ".so"))
  774. continue;
  775. /* if there is already a module by this name in the module_list,
  776. skip this file */
  777. if (find_resource(dirent->d_name, 0))
  778. continue;
  779. add_to_load_order(dirent->d_name, &load_order);
  780. }
  781. closedir(dir);
  782. } else {
  783. if (!ast_opt_quiet)
  784. ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
  785. ast_config_AST_MODULE_DIR);
  786. }
  787. #endif
  788. }
  789. /* now scan the config for any modules we are prohibited from loading and
  790. remove them from the load order */
  791. for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
  792. if (strcasecmp(v->name, "noload"))
  793. continue;
  794. AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
  795. if (!resource_name_match(order->resource, v->value)) {
  796. AST_LIST_REMOVE_CURRENT(entry);
  797. ast_free(order->resource);
  798. ast_free(order);
  799. }
  800. }
  801. AST_LIST_TRAVERSE_SAFE_END;
  802. }
  803. /* we are done with the config now, all the information we need is in the
  804. load_order list */
  805. ast_config_destroy(cfg);
  806. load_count = 0;
  807. AST_LIST_TRAVERSE(&load_order, order, entry)
  808. load_count++;
  809. if (load_count)
  810. ast_log(LOG_NOTICE, "%d modules will be loaded.\n", load_count);
  811. for (load_pass = 0; load_pass < LOAD_DONE; load_pass++) {
  812. AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
  813. switch (load_resource(order->resource, load_pass)) {
  814. case AST_MODULE_LOAD_SUCCESS:
  815. modulecount++;
  816. case AST_MODULE_LOAD_DECLINE:
  817. AST_LIST_REMOVE_CURRENT(entry);
  818. ast_free(order->resource);
  819. ast_free(order);
  820. break;
  821. case AST_MODULE_LOAD_FAILURE:
  822. res = -1;
  823. goto done;
  824. case AST_MODULE_LOAD_SKIP:
  825. /*
  826. * Try again later. This result is received when a module is
  827. * deferred because it is not a part of the current pass.
  828. */
  829. break;
  830. }
  831. }
  832. AST_LIST_TRAVERSE_SAFE_END;
  833. }
  834. done:
  835. while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
  836. ast_free(order->resource);
  837. ast_free(order);
  838. }
  839. AST_LIST_UNLOCK(&module_list);
  840. /* Tell manager clients that are aggressive at logging in that we're done
  841. loading modules. If there's a DNS problem in chan_sip, we might not
  842. even reach this */
  843. manager_event(EVENT_FLAG_SYSTEM, "ModuleLoadReport", "ModuleLoadStatus: Done\r\nModuleSelection: %s\r\nModuleCount: %d\r\n", preload_only ? "Preload" : "All", modulecount);
  844. return res;
  845. }
  846. void ast_update_use_count(void)
  847. {
  848. /* Notify any module monitors that the use count for a
  849. resource has changed */
  850. struct loadupdate *m;
  851. AST_LIST_LOCK(&updaters);
  852. AST_LIST_TRAVERSE(&updaters, m, entry)
  853. m->updater();
  854. AST_LIST_UNLOCK(&updaters);
  855. }
  856. int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
  857. const char *like)
  858. {
  859. struct ast_module *cur;
  860. int unlock = -1;
  861. int total_mod_loaded = 0;
  862. if (AST_LIST_TRYLOCK(&module_list))
  863. unlock = 0;
  864. AST_LIST_TRAVERSE(&module_list, cur, entry) {
  865. total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
  866. }
  867. if (unlock)
  868. AST_LIST_UNLOCK(&module_list);
  869. return total_mod_loaded;
  870. }
  871. /*! \brief Check if module exists */
  872. int ast_module_check(const char *name)
  873. {
  874. struct ast_module *cur;
  875. if (ast_strlen_zero(name))
  876. return 0; /* FALSE */
  877. cur = find_resource(name, 1);
  878. return (cur != NULL);
  879. }
  880. int ast_loader_register(int (*v)(void))
  881. {
  882. struct loadupdate *tmp;
  883. if (!(tmp = ast_malloc(sizeof(*tmp))))
  884. return -1;
  885. tmp->updater = v;
  886. AST_LIST_LOCK(&updaters);
  887. AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
  888. AST_LIST_UNLOCK(&updaters);
  889. return 0;
  890. }
  891. int ast_loader_unregister(int (*v)(void))
  892. {
  893. struct loadupdate *cur;
  894. AST_LIST_LOCK(&updaters);
  895. AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
  896. if (cur->updater == v) {
  897. AST_LIST_REMOVE_CURRENT(entry);
  898. break;
  899. }
  900. }
  901. AST_LIST_TRAVERSE_SAFE_END;
  902. AST_LIST_UNLOCK(&updaters);
  903. return cur ? 0 : -1;
  904. }
  905. struct ast_module *ast_module_ref(struct ast_module *mod)
  906. {
  907. ast_atomic_fetchadd_int(&mod->usecount, +1);
  908. ast_update_use_count();
  909. return mod;
  910. }
  911. void ast_module_unref(struct ast_module *mod)
  912. {
  913. ast_atomic_fetchadd_int(&mod->usecount, -1);
  914. ast_update_use_count();
  915. }