Patchwork [RFC,v2,4/6] module: implement module loading function

login
register
mail settings
Submitter Fam Zheng
Date Sept. 6, 2013, 7:28 a.m.
Message ID <1378452491-20467-5-git-send-email-famz@redhat.com>
Download mbox | patch
Permalink /patch/273110/
State New
Headers show

Comments

Fam Zheng - Sept. 6, 2013, 7:28 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             | 20 +++++++++++---------
 include/qemu/module.h |  9 +++++++++
 linux-user/main.c     |  3 +++
 qemu-img.c            |  1 +
 scripts/create_config |  4 ++++
 ui/console.c          |  1 +
 util/Makefile.objs    |  2 ++
 util/module.c         | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c                  |  2 ++
 11 files changed, 89 insertions(+), 9 deletions(-)
Paolo Bonzini - Sept. 6, 2013, 9:47 a.m.
Il 06/09/2013 09:28, Fam Zheng ha scritto:
> 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             | 20 +++++++++++---------
>  include/qemu/module.h |  9 +++++++++
>  linux-user/main.c     |  3 +++
>  qemu-img.c            |  1 +
>  scripts/create_config |  4 ++++
>  ui/console.c          |  1 +
>  util/Makefile.objs    |  2 ++
>  util/module.c         | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  vl.c                  |  2 ++
>  11 files changed, 89 insertions(+), 9 deletions(-)
> 
> diff --git a/block.c b/block.c
> index a387c1a..49ab0ce 100644
> --- a/block.c
> +++ b/block.c
> @@ -4009,6 +4009,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 75abb87..7fec1c7 100755
> --- a/configure
> +++ b/configure
> @@ -2249,15 +2249,17 @@ if test "$mingw32" = yes; then
>  else
>      glib_req_ver=2.12
>  fi
> -if $pkg_config --atleast-version=$glib_req_ver gthread-2.0 > /dev/null 2>&1
> -then
> -    glib_cflags=`$pkg_config --cflags gthread-2.0 2>/dev/null`
> -    glib_libs=`$pkg_config --libs gthread-2.0 2>/dev/null`
> -    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 > /dev/null 2>&1
> +    then
> +        glib_cflags=`$pkg_config --cflags $i 2>/dev/null`
> +        glib_libs=`$pkg_config --libs $i 2>/dev/null`
> +        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 03859bc..9cbac14 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;
>  
> @@ -3547,6 +3548,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/qemu-img.c b/qemu-img.c
> index b9a848d..063e6bf 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -34,6 +34,7 @@
>  #include <getopt.h>
>  #include <stdio.h>
>  #include <stdarg.h>
> +#include "qemu/module.h"
>  
>  #ifdef _WIN32
>  #include <windows.h>
> 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/ui/console.c b/ui/console.c
> index aad4fc9..ad90950 100644
> --- a/ui/console.c
> +++ b/ui/console.c
> @@ -27,6 +27,7 @@
>  #include "qemu/timer.h"
>  #include "qmp-commands.h"
>  #include "sysemu/char.h"
> +#include "qemu/module.h"

Why?

>  
>  //#define DEBUG_CONSOLE
>  #define DEFAULT_BACKSCROLL 512
> diff --git a/util/Makefile.objs b/util/Makefile.objs
> index dc72ab0..33e56b0 100644
> --- a/util/Makefile.objs
> +++ b/util/Makefile.objs
> @@ -11,3 +11,5 @@ util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o
>  util-obj-y += qemu-option.o qemu-progress.o
>  util-obj-y += hexdump.o
>  util-obj-y += crc32c.o
> +
> +$(obj)/module.o-libs := -lglib
> diff --git a/util/module.c b/util/module.c
> index 7acc33d..7254b1a 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,53 @@ 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[1024];
> +    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)) {
> +            pstrcpy(fname, sizeof(fname), path);
> +            pstrcat(fname, sizeof(fname), ep->d_name);

Please use g_strdup_printf instead.

> +            g_module = g_module_open(fname, G_MODULE_BIND_LAZY);

Why G_MODULE_BIND_LAZY?  Performance, I guess?

Having G_MODULE_BIND_LOCAL is probably a good idea too.

Paolo

> +            if (!g_module) {
> +                fprintf(stderr, "Failed to open module file %s\n",
> +                        g_module_error());
> +                continue;
> +            }
> +            printf("Loaded module %s\n", fname);
> +        }
> +    }
> +}
> diff --git a/vl.c b/vl.c
> index dfbc071..716c2db 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);
>

Patch

diff --git a/block.c b/block.c
index a387c1a..49ab0ce 100644
--- a/block.c
+++ b/block.c
@@ -4009,6 +4009,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 75abb87..7fec1c7 100755
--- a/configure
+++ b/configure
@@ -2249,15 +2249,17 @@  if test "$mingw32" = yes; then
 else
     glib_req_ver=2.12
 fi
-if $pkg_config --atleast-version=$glib_req_ver gthread-2.0 > /dev/null 2>&1
-then
-    glib_cflags=`$pkg_config --cflags gthread-2.0 2>/dev/null`
-    glib_libs=`$pkg_config --libs gthread-2.0 2>/dev/null`
-    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 > /dev/null 2>&1
+    then
+        glib_cflags=`$pkg_config --cflags $i 2>/dev/null`
+        glib_libs=`$pkg_config --libs $i 2>/dev/null`
+        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 03859bc..9cbac14 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;
 
@@ -3547,6 +3548,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/qemu-img.c b/qemu-img.c
index b9a848d..063e6bf 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -34,6 +34,7 @@ 
 #include <getopt.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include "qemu/module.h"
 
 #ifdef _WIN32
 #include <windows.h>
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/ui/console.c b/ui/console.c
index aad4fc9..ad90950 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -27,6 +27,7 @@ 
 #include "qemu/timer.h"
 #include "qmp-commands.h"
 #include "sysemu/char.h"
+#include "qemu/module.h"
 
 //#define DEBUG_CONSOLE
 #define DEFAULT_BACKSCROLL 512
diff --git a/util/Makefile.objs b/util/Makefile.objs
index dc72ab0..33e56b0 100644
--- a/util/Makefile.objs
+++ b/util/Makefile.objs
@@ -11,3 +11,5 @@  util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o
 util-obj-y += qemu-option.o qemu-progress.o
 util-obj-y += hexdump.o
 util-obj-y += crc32c.o
+
+$(obj)/module.o-libs := -lglib
diff --git a/util/module.c b/util/module.c
index 7acc33d..7254b1a 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,53 @@  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[1024];
+    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)) {
+            pstrcpy(fname, sizeof(fname), path);
+            pstrcat(fname, sizeof(fname), ep->d_name);
+            g_module = g_module_open(fname, G_MODULE_BIND_LAZY);
+            if (!g_module) {
+                fprintf(stderr, "Failed to open module file %s\n",
+                        g_module_error());
+                continue;
+            }
+            printf("Loaded module %s\n", fname);
+        }
+    }
+}
diff --git a/vl.c b/vl.c
index dfbc071..716c2db 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);