summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/module.c189
1 files changed, 105 insertions, 84 deletions
diff --git a/util/module.c b/util/module.c
index ad89cd50dc..32e263163c 100644
--- a/util/module.c
+++ b/util/module.c
@@ -21,6 +21,7 @@
#include "qemu/module.h"
#include "qemu/cutils.h"
#include "qemu/config-file.h"
+#include "qapi/error.h"
#ifdef CONFIG_MODULE_UPGRADES
#include "qemu-version.h"
#endif
@@ -144,25 +145,22 @@ static bool module_check_arch(const QemuModinfo *modinfo)
return true;
}
-static int module_load_file(const char *fname, bool export_symbols)
+/*
+ * module_load_dso: attempt to load an existing dso file
+ *
+ * fname: full pathname of the file to load
+ * export_symbols: if true, add the symbols to the global name space
+ * errp: error to set.
+ *
+ * Return value: true on success, false on error, and errp will be set.
+ */
+static bool module_load_dso(const char *fname, bool export_symbols,
+ Error **errp)
{
GModule *g_module;
void (*sym)(void);
- const char *dsosuf = CONFIG_HOST_DSOSUF;
- int len = strlen(fname);
- int suf_len = strlen(dsosuf);
ModuleEntry *e, *next;
- int ret, flags;
-
- if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
- /* wrong suffix */
- ret = -EINVAL;
- goto out;
- }
- if (access(fname, F_OK)) {
- ret = -ENOENT;
- goto out;
- }
+ int flags;
assert(QTAILQ_EMPTY(&dso_init_list));
@@ -172,46 +170,38 @@ static int module_load_file(const char *fname, bool export_symbols)
}
g_module = g_module_open(fname, flags);
if (!g_module) {
- fprintf(stderr, "Failed to open module: %s\n",
- g_module_error());
- ret = -EINVAL;
- goto out;
+ error_setg(errp, "failed to open module: %s", g_module_error());
+ return false;
}
if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
- fprintf(stderr, "Failed to initialize module: %s\n",
- fname);
- /* Print some info if this is a QEMU module (but from different build),
- * this will make debugging user problems easier. */
+ error_setg(errp, "failed to initialize module: %s", fname);
+ /*
+ * Print some info if this is a QEMU module (but from different build),
+ * this will make debugging user problems easier.
+ */
if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
- fprintf(stderr,
- "Note: only modules from the same build can be loaded.\n");
+ error_append_hint(errp,
+ "Only modules from the same build can be loaded.\n");
}
g_module_close(g_module);
- ret = -EINVAL;
- } else {
- QTAILQ_FOREACH(e, &dso_init_list, node) {
- e->init();
- register_module_init(e->init, e->type);
- }
- ret = 0;
+ return false;
}
+ QTAILQ_FOREACH(e, &dso_init_list, node) {
+ e->init();
+ register_module_init(e->init, e->type);
+ }
trace_module_load_module(fname);
QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
QTAILQ_REMOVE(&dso_init_list, e, node);
g_free(e);
}
-out:
- return ret;
+ return true;
}
-#endif
-bool module_load(const char *prefix, const char *lib_name)
+int module_load(const char *prefix, const char *name, Error **errp)
{
- bool success = false;
-
-#ifdef CONFIG_MODULES
- char *fname = NULL;
+ int rv = -1;
#ifdef CONFIG_MODULE_UPGRADES
char *version_dir;
#endif
@@ -219,34 +209,52 @@ bool module_load(const char *prefix, const char *lib_name)
char *dirs[5];
char *module_name;
int i = 0, n_dirs = 0;
- int ret;
bool export_symbols = false;
static GHashTable *loaded_modules;
const QemuModinfo *modinfo;
const char **sl;
if (!g_module_supported()) {
- fprintf(stderr, "Module is not supported by system.\n");
- return false;
+ error_setg(errp, "%s", "this platform does not support GLib modules");
+ return -1;
}
if (!loaded_modules) {
loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
}
- module_name = g_strdup_printf("%s%s", prefix, lib_name);
+ /* allocate all resources managed by the out: label here */
+ module_name = g_strdup_printf("%s%s", prefix, name);
if (g_hash_table_contains(loaded_modules, module_name)) {
g_free(module_name);
- return true;
+ return 2; /* module already loaded */
}
g_hash_table_add(loaded_modules, module_name);
+ search_dir = getenv("QEMU_MODULE_DIR");
+ if (search_dir != NULL) {
+ dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
+ }
+ dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
+
+#ifdef CONFIG_MODULE_UPGRADES
+ version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
+ G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
+ '_');
+ dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
+#endif
+ assert(n_dirs <= ARRAY_SIZE(dirs));
+
+ /* end of resources managed by the out: label */
+
for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
if (modinfo->arch) {
if (strcmp(modinfo->name, module_name) == 0) {
if (!module_check_arch(modinfo)) {
- return false;
+ error_setg(errp, "module arch does not match: "
+ "expected '%s', got '%s'", module_arch, modinfo->arch);
+ goto out;
}
}
}
@@ -254,7 +262,11 @@ bool module_load(const char *prefix, const char *lib_name)
if (strcmp(modinfo->name, module_name) == 0) {
/* we depend on other module(s) */
for (sl = modinfo->deps; *sl != NULL; sl++) {
- module_load("", *sl);
+ int subrv = module_load("", *sl, errp);
+ if (subrv <= 0) {
+ rv = subrv;
+ goto out;
+ }
}
} else {
for (sl = modinfo->deps; *sl != NULL; sl++) {
@@ -267,58 +279,52 @@ bool module_load(const char *prefix, const char *lib_name)
}
}
- search_dir = getenv("QEMU_MODULE_DIR");
- if (search_dir != NULL) {
- dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
- }
- dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
-
-#ifdef CONFIG_MODULE_UPGRADES
- version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
- G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
- '_');
- dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
-#endif
-
- assert(n_dirs <= ARRAY_SIZE(dirs));
-
for (i = 0; i < n_dirs; i++) {
- fname = g_strdup_printf("%s/%s%s",
- dirs[i], module_name, CONFIG_HOST_DSOSUF);
- ret = module_load_file(fname, export_symbols);
- g_free(fname);
- fname = NULL;
- /* Try loading until loaded a module file */
- if (!ret) {
- success = true;
- break;
+ char *fname = g_strdup_printf("%s/%s%s",
+ dirs[i], module_name, CONFIG_HOST_DSOSUF);
+ int ret = access(fname, F_OK);
+ if (ret != 0 && (errno == ENOENT || errno == ENOTDIR)) {
+ /*
+ * if we don't find the module in this dir, try the next one.
+ * If we don't find it in any dir, that can be fine too: user
+ * did not install the module. We will return 0 in this case
+ * with no error set.
+ */
+ g_free(fname);
+ continue;
+ } else if (ret != 0) {
+ /* most common is EACCES here */
+ error_setg_errno(errp, errno, "error trying to access %s", fname);
+ } else if (module_load_dso(fname, export_symbols, errp)) {
+ rv = 1; /* module successfully loaded */
}
+ g_free(fname);
+ goto out;
}
+ rv = 0; /* module not found */
- if (!success) {
+out:
+ if (rv <= 0) {
g_hash_table_remove(loaded_modules, module_name);
g_free(module_name);
}
-
for (i = 0; i < n_dirs; i++) {
g_free(dirs[i]);
}
-
-#endif
- return success;
+ return rv;
}
-#ifdef CONFIG_MODULES
-
static bool module_loaded_qom_all;
-void module_load_qom(const char *type)
+int module_load_qom(const char *type, Error **errp)
{
const QemuModinfo *modinfo;
const char **sl;
+ int rv = 0;
if (!type) {
- return;
+ error_setg(errp, "%s", "type is NULL");
+ return -1;
}
trace_module_lookup_object_type(type);
@@ -331,15 +337,24 @@ void module_load_qom(const char *type)
}
for (sl = modinfo->objs; *sl != NULL; sl++) {
if (strcmp(type, *sl) == 0) {
- module_load("", modinfo->name);
+ if (rv > 0) {
+ error_setg(errp, "multiple modules providing '%s'", type);
+ return -1;
+ }
+ rv = module_load("", modinfo->name, errp);
+ if (rv < 0) {
+ return rv;
+ }
}
}
}
+ return rv;
}
void module_load_qom_all(void)
{
const QemuModinfo *modinfo;
+ Error *local_err = NULL;
if (module_loaded_qom_all) {
return;
@@ -352,7 +367,9 @@ void module_load_qom_all(void)
if (!module_check_arch(modinfo)) {
continue;
}
- module_load("", modinfo->name);
+ if (module_load("", modinfo->name, &local_err) < 0) {
+ error_report_err(local_err);
+ }
}
module_loaded_qom_all = true;
}
@@ -368,7 +385,10 @@ void qemu_load_module_for_opts(const char *group)
}
for (sl = modinfo->opts; *sl != NULL; sl++) {
if (strcmp(group, *sl) == 0) {
- module_load("", modinfo->name);
+ Error *local_err = NULL;
+ if (module_load("", modinfo->name, &local_err) < 0) {
+ error_report_err(local_err);
+ }
}
}
}
@@ -378,7 +398,8 @@ void qemu_load_module_for_opts(const char *group)
void module_allow_arch(const char *arch) {}
void qemu_load_module_for_opts(const char *group) {}
-void module_load_qom(const char *type) {}
+int module_load(const char *prefix, const char *name, Error **errp) { return 2; }
+int module_load_qom(const char *type, Error **errp) { return 2; }
void module_load_qom_all(void) {}
#endif