]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Correctly initialize libvirt
authorJiri Denemark <jdenemar@redhat.com>
Mon, 9 May 2011 11:57:09 +0000 (13:57 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Tue, 17 May 2011 10:37:30 +0000 (12:37 +0200)
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
tools/virsh.c

index 5a5439d158639b48b04b809afc7dde36d5a53647..787908e86db74bd70039ec2194948aad6a36865c 100644 (file)
@@ -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
index a38750ffc849a95bd3e68d6dc8e31edd39eaf3db..2627cf47c3c057eeae299c3b7bb569f2f8609221 100644 (file)
@@ -12985,6 +12985,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 */
@@ -12998,15 +13002,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);
     }