module.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * QEMU Module Infrastructure
  3. *
  4. * Copyright IBM, Corp. 2009
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #ifdef CONFIG_MODULES
  17. #include <gmodule.h>
  18. #endif
  19. #include "qemu/queue.h"
  20. #include "qemu/module.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/config-file.h"
  23. #include "qapi/error.h"
  24. #ifdef CONFIG_MODULE_UPGRADES
  25. #include "qemu-version.h"
  26. #endif
  27. #include "trace.h"
  28. typedef struct ModuleEntry
  29. {
  30. void (*init)(void);
  31. QTAILQ_ENTRY(ModuleEntry) node;
  32. module_init_type type;
  33. } ModuleEntry;
  34. typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
  35. static ModuleTypeList init_type_list[MODULE_INIT_MAX];
  36. static bool modules_init_done[MODULE_INIT_MAX];
  37. static ModuleTypeList dso_init_list;
  38. static void init_lists(void)
  39. {
  40. static int inited;
  41. int i;
  42. if (inited) {
  43. return;
  44. }
  45. for (i = 0; i < MODULE_INIT_MAX; i++) {
  46. QTAILQ_INIT(&init_type_list[i]);
  47. }
  48. QTAILQ_INIT(&dso_init_list);
  49. inited = 1;
  50. }
  51. static ModuleTypeList *find_type(module_init_type type)
  52. {
  53. init_lists();
  54. return &init_type_list[type];
  55. }
  56. void register_module_init(void (*fn)(void), module_init_type type)
  57. {
  58. ModuleEntry *e;
  59. ModuleTypeList *l;
  60. e = g_malloc0(sizeof(*e));
  61. e->init = fn;
  62. e->type = type;
  63. l = find_type(type);
  64. QTAILQ_INSERT_TAIL(l, e, node);
  65. }
  66. void register_dso_module_init(void (*fn)(void), module_init_type type)
  67. {
  68. ModuleEntry *e;
  69. init_lists();
  70. e = g_malloc0(sizeof(*e));
  71. e->init = fn;
  72. e->type = type;
  73. QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
  74. }
  75. void module_call_init(module_init_type type)
  76. {
  77. ModuleTypeList *l;
  78. ModuleEntry *e;
  79. if (modules_init_done[type]) {
  80. return;
  81. }
  82. l = find_type(type);
  83. QTAILQ_FOREACH(e, l, node) {
  84. e->init();
  85. }
  86. modules_init_done[type] = true;
  87. }
  88. #ifdef CONFIG_MODULES
  89. static const QemuModinfo module_info_stub[] = { {
  90. /* end of list */
  91. } };
  92. static const QemuModinfo *module_info = module_info_stub;
  93. static const char *module_arch;
  94. void module_init_info(const QemuModinfo *info)
  95. {
  96. module_info = info;
  97. }
  98. void module_allow_arch(const char *arch)
  99. {
  100. module_arch = arch;
  101. }
  102. static bool module_check_arch(const QemuModinfo *modinfo)
  103. {
  104. if (modinfo->arch) {
  105. if (!module_arch) {
  106. /* no arch set -> ignore all */
  107. return false;
  108. }
  109. if (strcmp(module_arch, modinfo->arch) != 0) {
  110. /* mismatch */
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. /*
  117. * module_load_dso: attempt to load an existing dso file
  118. *
  119. * fname: full pathname of the file to load
  120. * export_symbols: if true, add the symbols to the global name space
  121. * errp: error to set.
  122. *
  123. * Return value: true on success, false on error, and errp will be set.
  124. */
  125. static bool module_load_dso(const char *fname, bool export_symbols,
  126. Error **errp)
  127. {
  128. GModule *g_module;
  129. void (*sym)(void);
  130. ModuleEntry *e, *next;
  131. int flags;
  132. assert(QTAILQ_EMPTY(&dso_init_list));
  133. flags = 0;
  134. if (!export_symbols) {
  135. flags |= G_MODULE_BIND_LOCAL;
  136. }
  137. g_module = g_module_open(fname, flags);
  138. if (!g_module) {
  139. error_setg(errp, "failed to open module: %s", g_module_error());
  140. return false;
  141. }
  142. if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
  143. error_setg(errp, "failed to initialize module: %s", fname);
  144. /*
  145. * Print some info if this is a QEMU module (but from different build),
  146. * this will make debugging user problems easier.
  147. */
  148. if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
  149. error_append_hint(errp,
  150. "Only modules from the same build can be loaded.\n");
  151. }
  152. g_module_close(g_module);
  153. return false;
  154. }
  155. QTAILQ_FOREACH(e, &dso_init_list, node) {
  156. e->init();
  157. register_module_init(e->init, e->type);
  158. }
  159. trace_module_load_module(fname);
  160. QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
  161. QTAILQ_REMOVE(&dso_init_list, e, node);
  162. g_free(e);
  163. }
  164. return true;
  165. }
  166. int module_load(const char *prefix, const char *name, Error **errp)
  167. {
  168. int rv = -1;
  169. #ifdef CONFIG_MODULE_UPGRADES
  170. char *version_dir;
  171. #endif
  172. const char *search_dir;
  173. char *dirs[5];
  174. char *module_name;
  175. int i = 0, n_dirs = 0;
  176. bool export_symbols = false;
  177. static GHashTable *loaded_modules;
  178. const QemuModinfo *modinfo;
  179. const char **sl;
  180. if (!g_module_supported()) {
  181. error_setg(errp, "%s", "this platform does not support GLib modules");
  182. return -1;
  183. }
  184. if (!loaded_modules) {
  185. loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
  186. }
  187. /* allocate all resources managed by the out: label here */
  188. module_name = g_strdup_printf("%s%s", prefix, name);
  189. if (g_hash_table_contains(loaded_modules, module_name)) {
  190. g_free(module_name);
  191. return 2; /* module already loaded */
  192. }
  193. g_hash_table_add(loaded_modules, module_name);
  194. search_dir = getenv("QEMU_MODULE_DIR");
  195. if (search_dir != NULL) {
  196. dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
  197. }
  198. dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
  199. #ifdef CONFIG_MODULE_UPGRADES
  200. version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
  201. G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
  202. '_');
  203. dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
  204. #endif
  205. assert(n_dirs <= ARRAY_SIZE(dirs));
  206. /* end of resources managed by the out: label */
  207. for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
  208. if (modinfo->arch) {
  209. if (strcmp(modinfo->name, module_name) == 0) {
  210. if (!module_check_arch(modinfo)) {
  211. error_setg(errp, "module arch does not match: "
  212. "expected '%s', got '%s'", module_arch, modinfo->arch);
  213. goto out;
  214. }
  215. }
  216. }
  217. if (modinfo->deps) {
  218. if (strcmp(modinfo->name, module_name) == 0) {
  219. /* we depend on other module(s) */
  220. for (sl = modinfo->deps; *sl != NULL; sl++) {
  221. int subrv = module_load("", *sl, errp);
  222. if (subrv <= 0) {
  223. rv = subrv;
  224. goto out;
  225. }
  226. }
  227. } else {
  228. for (sl = modinfo->deps; *sl != NULL; sl++) {
  229. if (strcmp(module_name, *sl) == 0) {
  230. /* another module depends on us */
  231. export_symbols = true;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. for (i = 0; i < n_dirs; i++) {
  238. char *fname = g_strdup_printf("%s/%s%s",
  239. dirs[i], module_name, CONFIG_HOST_DSOSUF);
  240. int ret = access(fname, F_OK);
  241. if (ret != 0 && (errno == ENOENT || errno == ENOTDIR)) {
  242. /*
  243. * if we don't find the module in this dir, try the next one.
  244. * If we don't find it in any dir, that can be fine too: user
  245. * did not install the module. We will return 0 in this case
  246. * with no error set.
  247. */
  248. g_free(fname);
  249. continue;
  250. } else if (ret != 0) {
  251. /* most common is EACCES here */
  252. error_setg_errno(errp, errno, "error trying to access %s", fname);
  253. } else if (module_load_dso(fname, export_symbols, errp)) {
  254. rv = 1; /* module successfully loaded */
  255. }
  256. g_free(fname);
  257. goto out;
  258. }
  259. rv = 0; /* module not found */
  260. out:
  261. if (rv <= 0) {
  262. g_hash_table_remove(loaded_modules, module_name);
  263. g_free(module_name);
  264. }
  265. for (i = 0; i < n_dirs; i++) {
  266. g_free(dirs[i]);
  267. }
  268. return rv;
  269. }
  270. static bool module_loaded_qom_all;
  271. int module_load_qom(const char *type, Error **errp)
  272. {
  273. const QemuModinfo *modinfo;
  274. const char **sl;
  275. int rv = 0;
  276. if (!type) {
  277. error_setg(errp, "%s", "type is NULL");
  278. return -1;
  279. }
  280. trace_module_lookup_object_type(type);
  281. for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
  282. if (!modinfo->objs) {
  283. continue;
  284. }
  285. if (!module_check_arch(modinfo)) {
  286. continue;
  287. }
  288. for (sl = modinfo->objs; *sl != NULL; sl++) {
  289. if (strcmp(type, *sl) == 0) {
  290. if (rv > 0) {
  291. error_setg(errp, "multiple modules providing '%s'", type);
  292. return -1;
  293. }
  294. rv = module_load("", modinfo->name, errp);
  295. if (rv < 0) {
  296. return rv;
  297. }
  298. }
  299. }
  300. }
  301. return rv;
  302. }
  303. void module_load_qom_all(void)
  304. {
  305. const QemuModinfo *modinfo;
  306. Error *local_err = NULL;
  307. if (module_loaded_qom_all) {
  308. return;
  309. }
  310. for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
  311. if (!modinfo->objs) {
  312. continue;
  313. }
  314. if (!module_check_arch(modinfo)) {
  315. continue;
  316. }
  317. if (module_load("", modinfo->name, &local_err) < 0) {
  318. error_report_err(local_err);
  319. }
  320. }
  321. module_loaded_qom_all = true;
  322. }
  323. void qemu_load_module_for_opts(const char *group)
  324. {
  325. const QemuModinfo *modinfo;
  326. const char **sl;
  327. for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
  328. if (!modinfo->opts) {
  329. continue;
  330. }
  331. for (sl = modinfo->opts; *sl != NULL; sl++) {
  332. if (strcmp(group, *sl) == 0) {
  333. Error *local_err = NULL;
  334. if (module_load("", modinfo->name, &local_err) < 0) {
  335. error_report_err(local_err);
  336. }
  337. }
  338. }
  339. }
  340. }
  341. #else
  342. void module_allow_arch(const char *arch) {}
  343. void qemu_load_module_for_opts(const char *group) {}
  344. int module_load(const char *prefix, const char *name, Error **errp) { return 2; }
  345. int module_load_qom(const char *type, Error **errp) { return 2; }
  346. void module_load_qom_all(void) {}
  347. #endif