module.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #ifdef CONFIG_MODULE_UPGRADES
  22. #include "qemu-version.h"
  23. #endif
  24. typedef struct ModuleEntry
  25. {
  26. void (*init)(void);
  27. QTAILQ_ENTRY(ModuleEntry) node;
  28. module_init_type type;
  29. } ModuleEntry;
  30. typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
  31. static ModuleTypeList init_type_list[MODULE_INIT_MAX];
  32. static bool modules_init_done[MODULE_INIT_MAX];
  33. static ModuleTypeList dso_init_list;
  34. static void init_lists(void)
  35. {
  36. static int inited;
  37. int i;
  38. if (inited) {
  39. return;
  40. }
  41. for (i = 0; i < MODULE_INIT_MAX; i++) {
  42. QTAILQ_INIT(&init_type_list[i]);
  43. }
  44. QTAILQ_INIT(&dso_init_list);
  45. inited = 1;
  46. }
  47. static ModuleTypeList *find_type(module_init_type type)
  48. {
  49. init_lists();
  50. return &init_type_list[type];
  51. }
  52. void register_module_init(void (*fn)(void), module_init_type type)
  53. {
  54. ModuleEntry *e;
  55. ModuleTypeList *l;
  56. e = g_malloc0(sizeof(*e));
  57. e->init = fn;
  58. e->type = type;
  59. l = find_type(type);
  60. QTAILQ_INSERT_TAIL(l, e, node);
  61. }
  62. void register_dso_module_init(void (*fn)(void), module_init_type type)
  63. {
  64. ModuleEntry *e;
  65. init_lists();
  66. e = g_malloc0(sizeof(*e));
  67. e->init = fn;
  68. e->type = type;
  69. QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
  70. }
  71. void module_call_init(module_init_type type)
  72. {
  73. ModuleTypeList *l;
  74. ModuleEntry *e;
  75. if (modules_init_done[type]) {
  76. return;
  77. }
  78. l = find_type(type);
  79. QTAILQ_FOREACH(e, l, node) {
  80. e->init();
  81. }
  82. modules_init_done[type] = true;
  83. }
  84. #ifdef CONFIG_MODULES
  85. static int module_load_file(const char *fname)
  86. {
  87. GModule *g_module;
  88. void (*sym)(void);
  89. const char *dsosuf = CONFIG_HOST_DSOSUF;
  90. int len = strlen(fname);
  91. int suf_len = strlen(dsosuf);
  92. ModuleEntry *e, *next;
  93. int ret;
  94. if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
  95. /* wrong suffix */
  96. ret = -EINVAL;
  97. goto out;
  98. }
  99. if (access(fname, F_OK)) {
  100. ret = -ENOENT;
  101. goto out;
  102. }
  103. assert(QTAILQ_EMPTY(&dso_init_list));
  104. g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
  105. if (!g_module) {
  106. fprintf(stderr, "Failed to open module: %s\n",
  107. g_module_error());
  108. ret = -EINVAL;
  109. goto out;
  110. }
  111. if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
  112. fprintf(stderr, "Failed to initialize module: %s\n",
  113. fname);
  114. /* Print some info if this is a QEMU module (but from different build),
  115. * this will make debugging user problems easier. */
  116. if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
  117. fprintf(stderr,
  118. "Note: only modules from the same build can be loaded.\n");
  119. }
  120. g_module_close(g_module);
  121. ret = -EINVAL;
  122. } else {
  123. QTAILQ_FOREACH(e, &dso_init_list, node) {
  124. e->init();
  125. register_module_init(e->init, e->type);
  126. }
  127. ret = 0;
  128. }
  129. QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
  130. QTAILQ_REMOVE(&dso_init_list, e, node);
  131. g_free(e);
  132. }
  133. out:
  134. return ret;
  135. }
  136. #endif
  137. bool module_load_one(const char *prefix, const char *lib_name)
  138. {
  139. bool success = false;
  140. #ifdef CONFIG_MODULES
  141. char *fname = NULL;
  142. #ifdef CONFIG_MODULE_UPGRADES
  143. char *version_dir;
  144. #endif
  145. const char *search_dir;
  146. char *dirs[5];
  147. char *module_name;
  148. int i = 0, n_dirs = 0;
  149. int ret;
  150. static GHashTable *loaded_modules;
  151. if (!g_module_supported()) {
  152. fprintf(stderr, "Module is not supported by system.\n");
  153. return false;
  154. }
  155. if (!loaded_modules) {
  156. loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
  157. }
  158. module_name = g_strdup_printf("%s%s", prefix, lib_name);
  159. if (!g_hash_table_add(loaded_modules, module_name)) {
  160. g_free(module_name);
  161. return true;
  162. }
  163. search_dir = getenv("QEMU_MODULE_DIR");
  164. if (search_dir != NULL) {
  165. dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
  166. }
  167. dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
  168. dirs[n_dirs++] = g_strdup(qemu_get_exec_dir());
  169. #ifdef CONFIG_MODULE_UPGRADES
  170. version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
  171. G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
  172. '_');
  173. dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
  174. #endif
  175. assert(n_dirs <= ARRAY_SIZE(dirs));
  176. for (i = 0; i < n_dirs; i++) {
  177. fname = g_strdup_printf("%s/%s%s",
  178. dirs[i], module_name, CONFIG_HOST_DSOSUF);
  179. ret = module_load_file(fname);
  180. g_free(fname);
  181. fname = NULL;
  182. /* Try loading until loaded a module file */
  183. if (!ret) {
  184. success = true;
  185. break;
  186. }
  187. }
  188. if (!success) {
  189. g_hash_table_remove(loaded_modules, module_name);
  190. g_free(module_name);
  191. }
  192. for (i = 0; i < n_dirs; i++) {
  193. g_free(dirs[i]);
  194. }
  195. #endif
  196. return success;
  197. }
  198. /*
  199. * Building devices and other qom objects modular is mostly useful in
  200. * case they have dependencies to external shared libraries, so we can
  201. * cut down the core qemu library dependencies. Which is the case for
  202. * only a very few devices & objects.
  203. *
  204. * So with the expectation that this will be rather the exception than
  205. * to rule and the list will not gain that many entries go with a
  206. * simple manually maintained list for now.
  207. */
  208. static struct {
  209. const char *type;
  210. const char *prefix;
  211. const char *module;
  212. } const qom_modules[] = {
  213. { "ccid-card-passthru", "hw-", "usb-smartcard" },
  214. { "ccid-card-emulated", "hw-", "usb-smartcard" },
  215. { "usb-redir", "hw-", "usb-redirect" },
  216. { "qxl-vga", "hw-", "display-qxl" },
  217. { "qxl", "hw-", "display-qxl" },
  218. { "virtio-gpu-device", "hw-", "display-virtio-gpu" },
  219. { "vhost-user-gpu", "hw-", "display-virtio-gpu" },
  220. { "chardev-braille", "chardev-", "baum" },
  221. };
  222. static bool module_loaded_qom_all;
  223. void module_load_qom_one(const char *type)
  224. {
  225. int i;
  226. if (!type) {
  227. return;
  228. }
  229. if (module_loaded_qom_all) {
  230. return;
  231. }
  232. for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
  233. if (strcmp(qom_modules[i].type, type) == 0) {
  234. module_load_one(qom_modules[i].prefix,
  235. qom_modules[i].module);
  236. return;
  237. }
  238. }
  239. }
  240. void module_load_qom_all(void)
  241. {
  242. int i;
  243. if (module_loaded_qom_all) {
  244. return;
  245. }
  246. for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
  247. if (i > 0 && (strcmp(qom_modules[i - 1].module,
  248. qom_modules[i].module) == 0 &&
  249. strcmp(qom_modules[i - 1].prefix,
  250. qom_modules[i].prefix) == 0)) {
  251. /* one module implementing multiple types -> load only once */
  252. continue;
  253. }
  254. module_load_one(qom_modules[i].prefix, qom_modules[i].module);
  255. }
  256. module_loaded_qom_all = true;
  257. }