loader.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Module Loader
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <dirent.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/options.h>
  20. #include <asterisk/config.h>
  21. #include <asterisk/logger.h>
  22. #include <asterisk/channel.h>
  23. #include <asterisk/term.h>
  24. #include <asterisk/manager.h>
  25. #include <asterisk/enum.h>
  26. #include <asterisk/rtp.h>
  27. #ifdef __APPLE__
  28. #include <asterisk/dlfcn-compat.h>
  29. #else
  30. #include <dlfcn.h>
  31. #endif
  32. #include <asterisk/md5.h>
  33. #include <pthread.h>
  34. #include "asterisk.h"
  35. #include "astconf.h"
  36. #ifndef RTLD_NOW
  37. #define RTLD_NOW 0
  38. #endif
  39. static char expected_key[] =
  40. { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
  41. 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
  42. struct module {
  43. int (*load_module)(void);
  44. int (*unload_module)(void);
  45. int (*usecount)(void);
  46. char *(*description)(void);
  47. char *(*key)(void);
  48. int (*reload)(void);
  49. void *lib;
  50. char resource[256];
  51. struct module *next;
  52. };
  53. static int printdigest(unsigned char *d)
  54. {
  55. int x;
  56. char buf[256];
  57. char buf2[16];
  58. snprintf(buf, sizeof(buf), "Unexpected signature:");
  59. for (x=0;x<16;x++) {
  60. snprintf(buf2, sizeof(buf2), " %02x", *(d++));
  61. strcat(buf, buf2);
  62. }
  63. strcat(buf, "\n");
  64. ast_log(LOG_DEBUG, buf);
  65. return 0;
  66. }
  67. static int key_matches(char *key1, char *key2)
  68. {
  69. int match = 1;
  70. int x;
  71. for (x=0;x<16;x++) {
  72. match &= (key1[x] == key2[x]);
  73. }
  74. return match;
  75. }
  76. static int verify_key(char *key)
  77. {
  78. struct MD5Context c;
  79. char digest[16];
  80. MD5Init(&c);
  81. MD5Update(&c, key, strlen(key));
  82. MD5Final(digest, &c);
  83. if (key_matches(expected_key, digest))
  84. return 0;
  85. printdigest(digest);
  86. return -1;
  87. }
  88. static struct loadupdate {
  89. int (*updater)(void);
  90. struct loadupdate *next;
  91. } *updaters = NULL;
  92. static ast_mutex_t modlock = AST_MUTEX_INITIALIZER;
  93. static ast_mutex_t reloadlock = AST_MUTEX_INITIALIZER;
  94. static struct module *module_list=NULL;
  95. int ast_unload_resource(char *resource_name, int force)
  96. {
  97. struct module *m, *ml = NULL;
  98. int res = -1;
  99. if (ast_mutex_lock(&modlock))
  100. ast_log(LOG_WARNING, "Failed to lock\n");
  101. m = module_list;
  102. while(m) {
  103. if (!strcasecmp(m->resource, resource_name)) {
  104. if ((res = m->usecount()) > 0) {
  105. if (force)
  106. ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
  107. else {
  108. ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
  109. ast_mutex_unlock(&modlock);
  110. return -1;
  111. }
  112. }
  113. res = m->unload_module();
  114. if (res) {
  115. ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
  116. if (force <= AST_FORCE_FIRM) {
  117. ast_mutex_unlock(&modlock);
  118. return -1;
  119. } else
  120. ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
  121. }
  122. if (ml)
  123. ml->next = m->next;
  124. else
  125. module_list = m->next;
  126. dlclose(m->lib);
  127. free(m);
  128. break;
  129. }
  130. ml = m;
  131. m = m->next;
  132. }
  133. ast_mutex_unlock(&modlock);
  134. ast_update_use_count();
  135. return res;
  136. }
  137. void ast_module_reload(void)
  138. {
  139. struct module *m;
  140. /* We'll do the logger and manager the favor of calling its reload here first */
  141. if (ast_mutex_trylock(&reloadlock)) {
  142. ast_verbose("The previous reload command didn't finish yet\n");
  143. return;
  144. }
  145. reload_manager();
  146. ast_enum_reload();
  147. ast_rtp_reload();
  148. time(&ast_lastreloadtime);
  149. ast_mutex_lock(&modlock);
  150. m = module_list;
  151. while(m) {
  152. if (m->reload) {
  153. if (option_verbose > 2)
  154. ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
  155. m->reload();
  156. }
  157. m = m->next;
  158. }
  159. ast_mutex_unlock(&modlock);
  160. ast_mutex_unlock(&reloadlock);
  161. }
  162. int ast_load_resource(char *resource_name)
  163. {
  164. static char fn[256];
  165. int errors=0;
  166. int res;
  167. struct module *m;
  168. int flags=RTLD_NOW;
  169. #ifdef RTLD_GLOBAL
  170. char *val;
  171. #endif
  172. char *key;
  173. int o;
  174. struct ast_config *cfg;
  175. char tmp[80];
  176. /* Keep the module file parsing silent */
  177. o = option_verbose;
  178. if (strncasecmp(resource_name, "res_", 4)) {
  179. option_verbose = 0;
  180. cfg = ast_load(AST_MODULE_CONFIG);
  181. option_verbose = o;
  182. if (cfg) {
  183. #ifdef RTLD_GLOBAL
  184. if ((val = ast_variable_retrieve(cfg, "global", resource_name))
  185. && ast_true(val))
  186. flags |= RTLD_GLOBAL;
  187. #endif
  188. ast_destroy(cfg);
  189. }
  190. } else {
  191. /* Resource modules are always loaded global and lazy */
  192. #ifdef RTLD_GLOBAL
  193. flags = (RTLD_GLOBAL | RTLD_LAZY);
  194. #else
  195. flags = RTLD_LAZY;
  196. #endif
  197. }
  198. if (ast_mutex_lock(&modlock))
  199. ast_log(LOG_WARNING, "Failed to lock\n");
  200. m = module_list;
  201. while(m) {
  202. if (!strcasecmp(m->resource, resource_name)) {
  203. ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
  204. ast_mutex_unlock(&modlock);
  205. return -1;
  206. }
  207. m = m->next;
  208. }
  209. m = malloc(sizeof(struct module));
  210. if (!m) {
  211. ast_log(LOG_WARNING, "Out of memory\n");
  212. ast_mutex_unlock(&modlock);
  213. return -1;
  214. }
  215. strncpy(m->resource, resource_name, sizeof(m->resource)-1);
  216. if (resource_name[0] == '/') {
  217. strncpy(fn, resource_name, sizeof(fn)-1);
  218. } else {
  219. snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
  220. }
  221. m->lib = dlopen(fn, flags);
  222. if (!m->lib) {
  223. ast_log(LOG_WARNING, "%s\n", dlerror());
  224. free(m);
  225. ast_mutex_unlock(&modlock);
  226. return -1;
  227. }
  228. m->load_module = dlsym(m->lib, "load_module");
  229. if (m->load_module == NULL)
  230. m->load_module = dlsym(m->lib, "_load_module");
  231. if (!m->load_module) {
  232. ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
  233. errors++;
  234. }
  235. m->unload_module = dlsym(m->lib, "unload_module");
  236. if (m->unload_module == NULL)
  237. m->unload_module = dlsym(m->lib, "_unload_module");
  238. if (!m->unload_module) {
  239. ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
  240. errors++;
  241. }
  242. m->usecount = dlsym(m->lib, "usecount");
  243. if (m->usecount == NULL)
  244. m->usecount = dlsym(m->lib, "_usecount");
  245. if (!m->usecount) {
  246. ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
  247. errors++;
  248. }
  249. m->description = dlsym(m->lib, "description");
  250. if (m->description == NULL)
  251. m->description = dlsym(m->lib, "_description");
  252. if (!m->description) {
  253. ast_log(LOG_WARNING, "No description in module %s\n", fn);
  254. errors++;
  255. }
  256. m->key = dlsym(m->lib, "key");
  257. if (m->key == NULL)
  258. m->key = dlsym(m->lib, "_key");
  259. if (!m->key) {
  260. ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
  261. errors++;
  262. }
  263. m->reload = dlsym(m->lib, "reload");
  264. if (m->reload == NULL)
  265. m->reload = dlsym(m->lib, "_reload");
  266. if (!m->key || !(key = m->key())) {
  267. ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
  268. key = NULL;
  269. errors++;
  270. }
  271. if (key && verify_key(key)) {
  272. ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
  273. errors++;
  274. }
  275. if (errors) {
  276. ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
  277. dlclose(m->lib);
  278. free(m);
  279. ast_mutex_unlock(&modlock);
  280. return -1;
  281. }
  282. if (!fully_booted) {
  283. if (option_verbose)
  284. ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
  285. if (option_console && !option_verbose)
  286. ast_verbose( ".");
  287. } else {
  288. if (option_verbose)
  289. ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
  290. }
  291. m->next = module_list;
  292. module_list = m;
  293. ast_mutex_unlock(&modlock);
  294. if ((res = m->load_module())) {
  295. ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
  296. ast_unload_resource(resource_name, 0);
  297. return -1;
  298. }
  299. ast_update_use_count();
  300. return 0;
  301. }
  302. static int ast_resource_exists(char *resource)
  303. {
  304. struct module *m;
  305. if (ast_mutex_lock(&modlock))
  306. ast_log(LOG_WARNING, "Failed to lock\n");
  307. m = module_list;
  308. while(m) {
  309. if (!strcasecmp(resource, m->resource))
  310. break;
  311. m = m->next;
  312. }
  313. ast_mutex_unlock(&modlock);
  314. if (m)
  315. return -1;
  316. else
  317. return 0;
  318. }
  319. int load_modules()
  320. {
  321. struct ast_config *cfg;
  322. struct ast_variable *v;
  323. char tmp[80];
  324. if (option_verbose)
  325. ast_verbose( "Asterisk Dynamic Loader Starting:\n");
  326. cfg = ast_load(AST_MODULE_CONFIG);
  327. if (cfg) {
  328. /* Load explicitly defined modules */
  329. v = ast_variable_browse(cfg, "modules");
  330. while(v) {
  331. if (!strcasecmp(v->name, "load")) {
  332. if (option_debug && !option_verbose)
  333. ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
  334. if (option_verbose) {
  335. ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
  336. fflush(stdout);
  337. }
  338. if (ast_load_resource(v->value)) {
  339. ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
  340. if (cfg)
  341. ast_destroy(cfg);
  342. return -1;
  343. }
  344. }
  345. v=v->next;
  346. }
  347. }
  348. if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
  349. /* Load all modules */
  350. DIR *mods;
  351. struct dirent *d;
  352. int x;
  353. /* Make two passes. First, load any resource modules, then load the others. */
  354. for (x=0;x<2;x++) {
  355. mods = opendir((char *)ast_config_AST_MODULE_DIR);
  356. if (mods) {
  357. while((d = readdir(mods))) {
  358. /* Must end in .so to load it. */
  359. if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
  360. !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
  361. !ast_resource_exists(d->d_name)) {
  362. /* It's a shared library -- Just be sure we're allowed to load it -- kinda
  363. an inefficient way to do it, but oh well. */
  364. if (cfg) {
  365. v = ast_variable_browse(cfg, "modules");
  366. while(v) {
  367. if (!strcasecmp(v->name, "noload") &&
  368. !strcasecmp(v->value, d->d_name))
  369. break;
  370. v = v->next;
  371. }
  372. if (v) {
  373. if (option_verbose) {
  374. ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
  375. fflush(stdout);
  376. }
  377. continue;
  378. }
  379. }
  380. if (option_debug && !option_verbose)
  381. ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
  382. if (option_verbose) {
  383. ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
  384. fflush(stdout);
  385. }
  386. if (ast_load_resource(d->d_name)) {
  387. ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
  388. if (cfg)
  389. ast_destroy(cfg);
  390. return -1;
  391. }
  392. }
  393. }
  394. closedir(mods);
  395. } else {
  396. if (!option_quiet)
  397. ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
  398. }
  399. }
  400. }
  401. ast_destroy(cfg);
  402. return 0;
  403. }
  404. void ast_update_use_count(void)
  405. {
  406. /* Notify any module monitors that the use count for a
  407. resource has changed */
  408. struct loadupdate *m;
  409. if (ast_mutex_lock(&modlock))
  410. ast_log(LOG_WARNING, "Failed to lock\n");
  411. m = updaters;
  412. while(m) {
  413. m->updater();
  414. m = m->next;
  415. }
  416. ast_mutex_unlock(&modlock);
  417. }
  418. int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
  419. {
  420. struct module *m;
  421. int unlock = -1;
  422. if (ast_mutex_trylock(&modlock))
  423. unlock = 0;
  424. m = module_list;
  425. while(m) {
  426. modentry(m->resource, m->description(), m->usecount());
  427. m = m->next;
  428. }
  429. if (unlock)
  430. ast_mutex_unlock(&modlock);
  431. return 0;
  432. }
  433. int ast_loader_register(int (*v)(void))
  434. {
  435. struct loadupdate *tmp;
  436. /* XXX Should be more flexible here, taking > 1 verboser XXX */
  437. if ((tmp = malloc(sizeof (struct loadupdate)))) {
  438. tmp->updater = v;
  439. if (ast_mutex_lock(&modlock))
  440. ast_log(LOG_WARNING, "Failed to lock\n");
  441. tmp->next = updaters;
  442. updaters = tmp;
  443. ast_mutex_unlock(&modlock);
  444. return 0;
  445. }
  446. return -1;
  447. }
  448. int ast_loader_unregister(int (*v)(void))
  449. {
  450. int res = -1;
  451. struct loadupdate *tmp, *tmpl=NULL;
  452. if (ast_mutex_lock(&modlock))
  453. ast_log(LOG_WARNING, "Failed to lock\n");
  454. tmp = updaters;
  455. while(tmp) {
  456. if (tmp->updater == v) {
  457. if (tmpl)
  458. tmpl->next = tmp->next;
  459. else
  460. updaters = tmp->next;
  461. break;
  462. }
  463. tmpl = tmp;
  464. tmp = tmp->next;
  465. }
  466. if (tmp)
  467. res = 0;
  468. ast_mutex_unlock(&modlock);
  469. return res;
  470. }