Patchwork [RFC,v3,4/5] module: implement module loading function

login
register
mail settings
Submitter Fam Zheng
Date Sept. 10, 2013, 1:02 a.m.
Message ID <1378774978-22602-5-git-send-email-famz@redhat.com>
Download mbox | patch
Permalink /patch/273741/
State New
Headers show

Comments

Fam Zheng - Sept. 10, 2013, 1:02 a.m.
Added three types of modules:

    typedef enum {
        MODULE_LOAD_BLOCK = 0,
        MODULE_LOAD_UI,
        MODULE_LOAD_NET,
        MODULE_LOAD_MAX,
    } module_load_type;

and their loading function:

    void module_load(module_load_type).

which loads all ".so" files in a subdir under "${PREFIX}/qemu/", e.g.
"/usr/lib/qemu/block". Modules of each type should be loaded before
respective subsystem initialization code.

Requires gmodule-2.0 from glib.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block.c               |  1 +
 bsd-user/main.c       |  3 +++
 configure             | 22 ++++++++++++---------
 include/qemu/module.h |  9 +++++++++
 linux-user/main.c     |  3 +++
 scripts/create_config |  4 ++++
 util/module.c         | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c                  |  2 ++
 8 files changed, 88 insertions(+), 9 deletions(-)

Patch

diff --git a/block.c b/block.c
index 26639e8..16ceaaf 100644
--- a/block.c
+++ b/block.c
@@ -4008,6 +4008,7 @@  BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
 
 void bdrv_init(void)
 {
+    module_load(MODULE_LOAD_BLOCK);
     module_call_init(MODULE_INIT_BLOCK);
 }
 
diff --git a/bsd-user/main.c b/bsd-user/main.c
index f9246aa..6cb9e35 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -33,6 +33,7 @@ 
 #include "tcg.h"
 #include "qemu/timer.h"
 #include "qemu/envlist.h"
+#include "qemu/module.h"
 
 int singlestep;
 #if defined(CONFIG_USE_GUEST_BASE)
@@ -749,6 +750,8 @@  int main(int argc, char **argv)
     if (argc <= 1)
         usage();
 
+    module_load(MODULE_LOAD_UI);
+    module_load(MODULE_LOAD_NET);
     module_call_init(MODULE_INIT_QOM);
 
     if ((envlist = envlist_create()) == NULL) {
diff --git a/configure b/configure
index c6d4a62..a2858c2 100755
--- a/configure
+++ b/configure
@@ -2252,15 +2252,19 @@  if test "$mingw32" = yes; then
 else
     glib_req_ver=2.12
 fi
-if $pkg_config --atleast-version=$glib_req_ver gthread-2.0; then
-    glib_cflags=`$pkg_config --cflags gthread-2.0`
-    glib_libs=`$pkg_config --libs gthread-2.0`
-    CFLAGS="$glib_cflags $CFLAGS"
-    LIBS="$glib_libs $LIBS"
-    libs_qga="$glib_libs $libs_qga"
-else
-    error_exit "glib-$glib_req_ver required to compile QEMU"
-fi
+
+for i in gthread-2.0 gmodule-2.0; do
+    if $pkg_config --atleast-version=$glib_req_ver $i; then
+        glib_cflags=`$pkg_config --cflags $i`
+        glib_libs=`$pkg_config --libs $i`
+        CFLAGS="$glib_cflags $CFLAGS"
+        LIBS="$glib_libs $LIBS"
+        libs_qga="$glib_libs $libs_qga"
+    else
+        error_exit "glib-$glib_req_ver required to compile QEMU"
+    fi
+done
+
 
 ##########################################
 # pixman support probe
diff --git a/include/qemu/module.h b/include/qemu/module.h
index c4ccd57..f00bc25 100644
--- a/include/qemu/module.h
+++ b/include/qemu/module.h
@@ -37,4 +37,13 @@  void register_module_init(void (*fn)(void), module_init_type type);
 
 void module_call_init(module_init_type type);
 
+typedef enum {
+    MODULE_LOAD_BLOCK = 0,
+    MODULE_LOAD_UI,
+    MODULE_LOAD_NET,
+    MODULE_LOAD_MAX,
+} module_load_type;
+
+void module_load(module_load_type type);
+
 #endif
diff --git a/linux-user/main.c b/linux-user/main.c
index 5c2f7b2..db08c23 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -34,6 +34,7 @@ 
 #include "qemu/timer.h"
 #include "qemu/envlist.h"
 #include "elf.h"
+#include <qemu/module.h>
 
 char *exec_path;
 
@@ -3551,6 +3552,8 @@  int main(int argc, char **argv, char **envp)
     int i;
     int ret;
 
+    module_load(MODULE_LOAD_UI);
+    module_load(MODULE_LOAD_NET);
     module_call_init(MODULE_INIT_QOM);
 
     qemu_cache_utils_init(envp);
diff --git a/scripts/create_config b/scripts/create_config
index b1adbf5..7a54f2d 100755
--- a/scripts/create_config
+++ b/scripts/create_config
@@ -25,6 +25,7 @@  case $line in
  prefix=*)
     # save for the next definitions
     prefix=${line#*=}
+    echo "#define CONFIG_PREFIX \"$prefix\""
     ;;
  CONFIG_AUDIO_DRIVERS=*)
     drivers=${line#*=}
@@ -104,6 +105,9 @@  case $line in
     value=${line#*=}
     echo "#define $name $value"
     ;;
+ DSOSUF=*)
+    echo "#define HOST_DSOSUF \"${line#*=}\""
+    ;;
 esac
 
 done # read
diff --git a/util/module.c b/util/module.c
index 7acc33d..ef75f8e 100644
--- a/util/module.c
+++ b/util/module.c
@@ -13,6 +13,8 @@ 
  * GNU GPL, version 2 or (at your option) any later version.
  */
 
+#include <gmodule.h>
+#include <dirent.h>
 #include "qemu-common.h"
 #include "qemu/queue.h"
 #include "qemu/module.h"
@@ -79,3 +81,54 @@  void module_call_init(module_init_type type)
         e->init();
     }
 }
+
+void module_load(module_load_type type)
+{
+    const char *path;
+    const char *dsosuf = HOST_DSOSUF;
+    char *fname;
+    int suf_len = strlen(dsosuf);
+    DIR *dp;
+    struct dirent *ep = NULL;
+    GModule *g_module;
+
+    if (!g_module_supported()) {
+        return;
+    }
+
+    switch (type) {
+    case MODULE_LOAD_BLOCK:
+        path = CONFIG_PREFIX "/qemu/block/";
+        break;
+    case MODULE_LOAD_UI:
+        path = CONFIG_PREFIX "/qemu/ui/";
+        break;
+    case MODULE_LOAD_NET:
+        path = CONFIG_PREFIX "/qemu/net/";
+        break;
+    default:
+        return;
+    }
+
+    dp = opendir(path);
+    if (!dp) {
+        fprintf(stderr, "Failed to open dir %s\n", path);
+        return;
+    }
+    for (ep = readdir(dp); ep; ep = readdir(dp)) {
+        int len = strlen(ep->d_name);
+        if (len > suf_len &&
+                !strcmp(&ep->d_name[len - suf_len], dsosuf)) {
+            fname = g_strdup_printf("%s%s", path, ep->d_name);
+            g_module = g_module_open(fname,
+                                     G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
+            if (!g_module) {
+                fprintf(stderr, "Failed to open module file %s\n",
+                        g_module_error());
+                g_free(fname);
+                continue;
+            }
+            g_free(fname);
+        }
+    }
+}
diff --git a/vl.c b/vl.c
index b4b119a..659e53a 100644
--- a/vl.c
+++ b/vl.c
@@ -2940,6 +2940,8 @@  int main(int argc, char **argv, char **envp)
 #endif
     }
 
+    module_load(MODULE_LOAD_UI);
+    module_load(MODULE_LOAD_NET);
     module_call_init(MODULE_INIT_QOM);
 
     qemu_add_opts(&qemu_drive_opts);