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
  23typedef struct ModuleEntry
  24{
  25    void (*init)(void);
  26    QTAILQ_ENTRY(ModuleEntry) node;
  27    module_init_type type;
  28} ModuleEntry;
  29
  30typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
  31
  32static ModuleTypeList init_type_list[MODULE_INIT_MAX];
  33
  34static ModuleTypeList dso_init_list;
  35
  36static void init_lists(void)
  37{
  38    static int inited;
  39    int i;
  40
  41    if (inited) {
  42        return;
  43    }
  44
  45    for (i = 0; i < MODULE_INIT_MAX; i++) {
  46        QTAILQ_INIT(&init_type_list[i]);
  47    }
  48
  49    QTAILQ_INIT(&dso_init_list);
  50
  51    inited = 1;
  52}
  53
  54
  55static ModuleTypeList *find_type(module_init_type type)
  56{
  57    init_lists();
  58
  59    return &init_type_list[type];
  60}
  61
  62void register_module_init(void (*fn)(void), module_init_type type)
  63{
  64    ModuleEntry *e;
  65    ModuleTypeList *l;
  66
  67    e = g_malloc0(sizeof(*e));
  68    e->init = fn;
  69    e->type = type;
  70
  71    l = find_type(type);
  72
  73    QTAILQ_INSERT_TAIL(l, e, node);
  74}
  75
  76void register_dso_module_init(void (*fn)(void), module_init_type type)
  77{
  78    ModuleEntry *e;
  79
  80    init_lists();
  81
  82    e = g_malloc0(sizeof(*e));
  83    e->init = fn;
  84    e->type = type;
  85
  86    QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
  87}
  88
  89void module_call_init(module_init_type type)
  90{
  91    ModuleTypeList *l;
  92    ModuleEntry *e;
  93
  94    l = find_type(type);
  95
  96    QTAILQ_FOREACH(e, l, node) {
  97        e->init();
  98    }
  99}
 100
 101#ifdef CONFIG_MODULES
 102static int module_load_file(const char *fname)
 103{
 104    GModule *g_module;
 105    void (*sym)(void);
 106    const char *dsosuf = HOST_DSOSUF;
 107    int len = strlen(fname);
 108    int suf_len = strlen(dsosuf);
 109    ModuleEntry *e, *next;
 110    int ret;
 111
 112    if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
 113        /* wrong suffix */
 114        ret = -EINVAL;
 115        goto out;
 116    }
 117    if (access(fname, F_OK)) {
 118        ret = -ENOENT;
 119        goto out;
 120    }
 121
 122    assert(QTAILQ_EMPTY(&dso_init_list));
 123
 124    g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
 125    if (!g_module) {
 126        fprintf(stderr, "Failed to open module: %s\n",
 127                g_module_error());
 128        ret = -EINVAL;
 129        goto out;
 130    }
 131    if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
 132        fprintf(stderr, "Failed to initialize module: %s\n",
 133                fname);
 134        /* Print some info if this is a QEMU module (but from different build),
 135         * this will make debugging user problems easier. */
 136        if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
 137            fprintf(stderr,
 138                    "Note: only modules from the same build can be loaded.\n");
 139        }
 140        g_module_close(g_module);
 141        ret = -EINVAL;
 142    } else {
 143        QTAILQ_FOREACH(e, &dso_init_list, node) {
 144            e->init();
 145            register_module_init(e->init, e->type);
 146        }
 147        ret = 0;
 148    }
 149
 150    QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
 151        QTAILQ_REMOVE(&dso_init_list, e, node);
 152        g_free(e);
 153    }
 154out:
 155    return ret;
 156}
 157#endif
 158
 159void module_load_one(const char *prefix, const char *lib_name)
 160{
 161#ifdef CONFIG_MODULES
 162    char *fname = NULL;
 163    char *exec_dir;
 164    const char *search_dir;
 165    char *dirs[4];
 166    char *module_name;
 167    int i = 0, n_dirs = 0;
 168    int ret;
 169    static GHashTable *loaded_modules;
 170
 171    if (!g_module_supported()) {
 172        fprintf(stderr, "Module is not supported by system.\n");
 173        return;
 174    }
 175
 176    if (!loaded_modules) {
 177        loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
 178    }
 179
 180    module_name = g_strdup_printf("%s%s", prefix, lib_name);
 181
 182    if (g_hash_table_lookup(loaded_modules, module_name)) {
 183        g_free(module_name);
 184        return;
 185    }
 186    g_hash_table_insert(loaded_modules, module_name, module_name);
 187
 188    exec_dir = qemu_get_exec_dir();
 189    search_dir = getenv("QEMU_MODULE_DIR");
 190    if (search_dir != NULL) {
 191        dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
 192    }
 193    dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
 194    dirs[n_dirs++] = g_strdup_printf("%s/..", exec_dir ? : "");
 195    dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : "");
 196    assert(n_dirs <= ARRAY_SIZE(dirs));
 197
 198    g_free(exec_dir);
 199    exec_dir = NULL;
 200
 201    for (i = 0; i < n_dirs; i++) {
 202        fname = g_strdup_printf("%s/%s%s",
 203                dirs[i], module_name, HOST_DSOSUF);
 204        ret = module_load_file(fname);
 205        g_free(fname);
 206        fname = NULL;
 207        /* Try loading until loaded a module file */
 208        if (!ret) {
 209            break;
 210        }
 211    }
 212
 213    for (i = 0; i < n_dirs; i++) {
 214        g_free(dirs[i]);
 215    }
 216
 217#endif
 218}
 219