qemu/util/module.c
<<
>>
Prefs
   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
  16#include "qemu/osdep.h"
  17#ifdef CONFIG_MODULES
  18#include <gmodule.h>
  19#endif
  20#include "qemu/queue.h"
  21#include "qemu/module.h"
  22#include "qemu/cutils.h"
  23#ifdef CONFIG_MODULE_UPGRADES
  24#include "qemu-version.h"
  25#endif
  26
  27typedef struct ModuleEntry
  28{
  29    void (*init)(void);
  30    QTAILQ_ENTRY(ModuleEntry) node;
  31    module_init_type type;
  32} ModuleEntry;
  33
  34typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
  35
  36static ModuleTypeList init_type_list[MODULE_INIT_MAX];
  37static bool modules_init_done[MODULE_INIT_MAX];
  38
  39static ModuleTypeList dso_init_list;
  40
  41static void init_lists(void)
  42{
  43    static int inited;
  44    int i;
  45
  46    if (inited) {
  47        return;
  48    }
  49
  50    for (i = 0; i < MODULE_INIT_MAX; i++) {
  51        QTAILQ_INIT(&init_type_list[i]);
  52    }
  53
  54    QTAILQ_INIT(&dso_init_list);
  55
  56    inited = 1;
  57}
  58
  59
  60static ModuleTypeList *find_type(module_init_type type)
  61{
  62    init_lists();
  63
  64    return &init_type_list[type];
  65}
  66
  67void register_module_init(void (*fn)(void), module_init_type type)
  68{
  69    ModuleEntry *e;
  70    ModuleTypeList *l;
  71
  72    e = g_malloc0(sizeof(*e));
  73    e->init = fn;
  74    e->type = type;
  75
  76    l = find_type(type);
  77
  78    QTAILQ_INSERT_TAIL(l, e, node);
  79}
  80
  81void register_dso_module_init(void (*fn)(void), module_init_type type)
  82{
  83    ModuleEntry *e;
  84
  85    init_lists();
  86
  87    e = g_malloc0(sizeof(*e));
  88    e->init = fn;
  89    e->type = type;
  90
  91    QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
  92}
  93
  94void module_call_init(module_init_type type)
  95{
  96    ModuleTypeList *l;
  97    ModuleEntry *e;
  98
  99    if (modules_init_done[type]) {
 100        return;
 101    }
 102
 103    l = find_type(type);
 104
 105    QTAILQ_FOREACH(e, l, node) {
 106        e->init();
 107    }
 108
 109    modules_init_done[type] = true;
 110}
 111
 112#ifdef CONFIG_MODULES
 113static int module_load_file(const char *fname, bool mayfail, bool export_symbols)
 114{
 115    GModule *g_module;
 116    void (*sym)(void);
 117    const char *dsosuf = CONFIG_HOST_DSOSUF;
 118    int len = strlen(fname);
 119    int suf_len = strlen(dsosuf);
 120    ModuleEntry *e, *next;
 121    int ret, flags;
 122
 123    if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
 124        /* wrong suffix */
 125        ret = -EINVAL;
 126        goto out;
 127    }
 128    if (access(fname, F_OK)) {
 129        ret = -ENOENT;
 130        goto out;
 131    }
 132
 133    assert(QTAILQ_EMPTY(&dso_init_list));
 134
 135    flags = 0;
 136    if (!export_symbols) {
 137        flags |= G_MODULE_BIND_LOCAL;
 138    }
 139    g_module = g_module_open(fname, flags);
 140    if (!g_module) {
 141        if (!mayfail) {
 142            fprintf(stderr, "Failed to open module: %s\n",
 143                    g_module_error());
 144        }
 145        ret = -EINVAL;
 146        goto out;
 147    }
 148    if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
 149        fprintf(stderr, "Failed to initialize module: %s\n",
 150                fname);
 151        /* Print some info if this is a QEMU module (but from different build),
 152         * this will make debugging user problems easier. */
 153        if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
 154            fprintf(stderr,
 155                    "Note: only modules from the same build can be loaded.\n");
 156        }
 157        g_module_close(g_module);
 158        ret = -EINVAL;
 159    } else {
 160        QTAILQ_FOREACH(e, &dso_init_list, node) {
 161            e->init();
 162            register_module_init(e->init, e->type);
 163        }
 164        ret = 0;
 165    }
 166
 167    QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
 168        QTAILQ_REMOVE(&dso_init_list, e, node);
 169        g_free(e);
 170    }
 171out:
 172    return ret;
 173}
 174
 175static const struct {
 176    const char *name;
 177    const char *dep;
 178} module_deps[] = {
 179    { "audio-spice",    "ui-spice-core" },
 180    { "chardev-spice",  "ui-spice-core" },
 181    { "hw-display-qxl", "ui-spice-core" },
 182    { "ui-spice-app",   "ui-spice-core" },
 183    { "ui-spice-app",   "chardev-spice" },
 184
 185#ifdef CONFIG_OPENGL
 186    { "ui-egl-headless", "ui-opengl"    },
 187    { "ui-gtk",          "ui-opengl"    },
 188    { "ui-sdl",          "ui-opengl"    },
 189    { "ui-spice-core",   "ui-opengl"    },
 190#endif
 191};
 192#endif
 193
 194bool module_load_one(const char *prefix, const char *lib_name, bool mayfail)
 195{
 196    bool success = false;
 197
 198#ifdef CONFIG_MODULES
 199    char *fname = NULL;
 200#ifdef CONFIG_MODULE_UPGRADES
 201    char *version_dir;
 202#endif
 203    const char *search_dir;
 204    char *dirs[5];
 205    char *module_name;
 206    int i = 0, n_dirs = 0;
 207    int ret, dep;
 208    bool export_symbols = false;
 209    static GHashTable *loaded_modules;
 210
 211    if (!g_module_supported()) {
 212        fprintf(stderr, "Module is not supported by system.\n");
 213        return false;
 214    }
 215
 216    if (!loaded_modules) {
 217        loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
 218    }
 219
 220    module_name = g_strdup_printf("%s%s", prefix, lib_name);
 221
 222    for (dep = 0; dep < ARRAY_SIZE(module_deps); dep++) {
 223        if (strcmp(module_name, module_deps[dep].name) == 0) {
 224            /* we depend on another module */
 225            module_load_one("", module_deps[dep].dep, false);
 226        }
 227        if (strcmp(module_name, module_deps[dep].dep) == 0) {
 228            /* another module depends on us */
 229            export_symbols = true;
 230        }
 231    }
 232
 233    if (g_hash_table_contains(loaded_modules, module_name)) {
 234        g_free(module_name);
 235        return true;
 236    }
 237    g_hash_table_add(loaded_modules, module_name);
 238
 239    search_dir = getenv("QEMU_MODULE_DIR");
 240    if (search_dir != NULL) {
 241        dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
 242    }
 243    dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
 244    dirs[n_dirs++] = g_strdup(qemu_get_exec_dir());
 245
 246#ifdef CONFIG_MODULE_UPGRADES
 247    version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
 248                             G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
 249                             '_');
 250    dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
 251#endif
 252
 253    assert(n_dirs <= ARRAY_SIZE(dirs));
 254
 255    for (i = 0; i < n_dirs; i++) {
 256        fname = g_strdup_printf("%s/%s%s",
 257                dirs[i], module_name, CONFIG_HOST_DSOSUF);
 258        ret = module_load_file(fname, mayfail, export_symbols);
 259        g_free(fname);
 260        fname = NULL;
 261        /* Try loading until loaded a module file */
 262        if (!ret) {
 263            success = true;
 264            break;
 265        }
 266    }
 267
 268    if (!success) {
 269        g_hash_table_remove(loaded_modules, module_name);
 270        g_free(module_name);
 271    }
 272
 273    for (i = 0; i < n_dirs; i++) {
 274        g_free(dirs[i]);
 275    }
 276
 277#endif
 278    return success;
 279}
 280
 281/*
 282 * Building devices and other qom objects modular is mostly useful in
 283 * case they have dependencies to external shared libraries, so we can
 284 * cut down the core qemu library dependencies.  Which is the case for
 285 * only a very few devices & objects.
 286 *
 287 * So with the expectation that this will be rather the exception than
 288 * the rule and the list will not gain that many entries, go with a
 289 * simple manually maintained list for now.
 290 *
 291 * The list must be sorted by module (module_load_qom_all() needs this).
 292 */
 293static struct {
 294    const char *type;
 295    const char *prefix;
 296    const char *module;
 297} const qom_modules[] = {
 298    { "ccid-card-passthru",    "hw-", "usb-smartcard"         },
 299    { "ccid-card-emulated",    "hw-", "usb-smartcard"         },
 300    { "usb-redir",             "hw-", "usb-redirect"          },
 301    { "qxl-vga",               "hw-", "display-qxl"           },
 302    { "qxl",                   "hw-", "display-qxl"           },
 303    { "virtio-gpu-device",     "hw-", "display-virtio-gpu"    },
 304    { "vhost-user-gpu",        "hw-", "display-virtio-gpu"    },
 305    { "virtio-gpu-pci-base",   "hw-", "display-virtio-gpu-pci" },
 306    { "virtio-gpu-pci",        "hw-", "display-virtio-gpu-pci" },
 307    { "vhost-user-gpu-pci",    "hw-", "display-virtio-gpu-pci" },
 308    { "virtio-gpu-ccw",        "hw-", "s390x-virtio-gpu-ccw"   },
 309    { "virtio-vga-base",       "hw-", "display-virtio-vga"    },
 310    { "virtio-vga",            "hw-", "display-virtio-vga"    },
 311    { "vhost-user-vga",        "hw-", "display-virtio-vga"    },
 312    { "chardev-braille",       "chardev-", "baum"             },
 313    { "chardev-spicevmc",      "chardev-", "spice"            },
 314    { "chardev-spiceport",     "chardev-", "spice"            },
 315};
 316
 317static bool module_loaded_qom_all;
 318
 319void module_load_qom_one(const char *type)
 320{
 321    int i;
 322
 323    if (!type) {
 324        return;
 325    }
 326    for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
 327        if (strcmp(qom_modules[i].type, type) == 0) {
 328            module_load_one(qom_modules[i].prefix,
 329                            qom_modules[i].module,
 330                            false);
 331            return;
 332        }
 333    }
 334}
 335
 336void module_load_qom_all(void)
 337{
 338    int i;
 339
 340    if (module_loaded_qom_all) {
 341        return;
 342    }
 343    for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
 344        if (i > 0 && (strcmp(qom_modules[i - 1].module,
 345                             qom_modules[i].module) == 0 &&
 346                      strcmp(qom_modules[i - 1].prefix,
 347                             qom_modules[i].prefix) == 0)) {
 348            /* one module implementing multiple types -> load only once */
 349            continue;
 350        }
 351        module_load_one(qom_modules[i].prefix, qom_modules[i].module, true);
 352    }
 353    module_loaded_qom_all = true;
 354}
 355