[libvirt] [PATCH v2] virsh: Correctly initialize libvirt

Jiri Denemark jdenemar at redhat.com
Mon May 16 14:59:48 UTC 2011


virsh didn't call virInitialize(), which (among other things)
initializes virLastErr thread local variable. As a result of that, virsh
could just segfault in virEventRegisterDefaultImpl() since that is the
first call that touches (resets) virLastErr.

I have no idea what lucky coincidence made this bug visible but I was
able to reproduce it in 100% cases but only in one specific environment
which included building in sandbox.
---
 src/libvirt.c |    3 +++
 tools/virsh.c |   13 +++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/libvirt.c b/src/libvirt.c
index 5a5439d..787908e 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -369,6 +369,9 @@ static struct gcry_thread_cbs virTLSThreadImpl = {
  * in multithreaded applications to avoid potential race when initializing
  * the library.
  *
+ * Calling virInitialize is mandatory, unless your first API call is one of
+ * virConnectOpen*.
+ *
  * Returns 0 in case of success, -1 in case of error
  */
 int
diff --git a/tools/virsh.c b/tools/virsh.c
index e35637d..b469e7a 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -12983,6 +12983,10 @@ main(int argc, char **argv)
     char *defaultConn;
     bool ret = true;
 
+    memset(ctl, 0, sizeof(vshControl));
+    ctl->imode = true;          /* default is interactive mode */
+    ctl->log_fd = -1;           /* Initialize log file descriptor */
+
     if (!setlocale(LC_ALL, "")) {
         perror("setlocale");
         /* failure to setup locale is not fatal */
@@ -12996,15 +13000,16 @@ main(int argc, char **argv)
         return EXIT_FAILURE;
     }
 
+    if (virInitialize() < 0) {
+        vshError(ctl, "%s", _("Failed to initialize libvirt"));
+        return EXIT_FAILURE;
+    }
+
     if (!(progname = strrchr(argv[0], '/')))
         progname = argv[0];
     else
         progname++;
 
-    memset(ctl, 0, sizeof(vshControl));
-    ctl->imode = true;          /* default is interactive mode */
-    ctl->log_fd = -1;           /* Initialize log file descriptor */
-
     if ((defaultConn = getenv("VIRSH_DEFAULT_CONNECT_URI"))) {
         ctl->name = vshStrdup(ctl, defaultConn);
     }
-- 
1.7.5.rc3




More information about the libvir-list mailing list