[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] (failed) virConnectOpenAuth: Add VIR_CONNECT_NO_DEFAULT_ERROR_HANDLER flag.



Well, it was a good idea, but the patch doesn't work.  Apparently
along their open paths, drivers don't set errors in the newly created
handle, but in the global handler instead (hence the errors still get
printed to stderr).

How much I hate virterror ...

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org
>From 6ef50cf04a8c9e020322d5df8f5c977da18a52e8 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones redhat com>
Date: Sat, 13 Oct 2012 18:52:07 +0100
Subject: [PATCH] virConnectOpenAuth: Add VIR_CONNECT_NO_DEFAULT_ERROR_HANDLER
 flag.

One particular bugbear of mine with the libvirt API is the fact that
you cannot completely hide errors that happen during virConnectOpen*
from being printed on stderr.  Code can capture such errors, eg by
doing:

  conn = virConnectOpen (NULL);
  if (!conn) {
    err = virGetLastError ();
    // pass 'err' up through your own logging mechanism
  }

but they are always printed on stderr as well.

This commit adds a virConnectOpenAuth flag,
VIR_CONNECT_NO_DEFAULT_ERROR_HANDLER,
which prevents the default error handler (print to stderr) from being
installed.  Instead errors are ignored until picked up by
virGetLastError.
---
 include/libvirt/libvirt.h.in |  2 ++
 src/libvirt.c                | 12 ++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index a4e8ca9..7e72bbd 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -1073,6 +1073,8 @@ typedef virNodeMemoryStats *virNodeMemoryStatsPtr;
 typedef enum {
     VIR_CONNECT_RO         = (1 << 0),  /* A readonly connection */
     VIR_CONNECT_NO_ALIASES = (1 << 1),  /* Don't try to resolve URI aliases */
+                                 /* Don't install the default error handler */
+    VIR_CONNECT_NO_DEFAULT_ERROR_HANDLER = (1 << 2),
 } virConnectFlags;
 
 
diff --git a/src/libvirt.c b/src/libvirt.c
index 3c6d67d..5f007af 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -1072,6 +1072,13 @@ cleanup:
     return ret;
 }
 
+static void
+ignore_errors (void *ignore ATTRIBUTE_UNUSED,
+               virErrorPtr ignore2 ATTRIBUTE_UNUSED)
+{
+    /* empty */
+}
+
 static virConnectPtr
 do_open (const char *name,
          virConnectAuthPtr auth,
@@ -1087,6 +1094,11 @@ do_open (const char *name,
     if (ret == NULL)
         return NULL;
 
+    if (flags & VIR_CONNECT_NO_DEFAULT_ERROR_HANDLER) {
+        virConnSetErrorFunc(ret, NULL, ignore_errors);
+        flags &= ~VIR_CONNECT_NO_DEFAULT_ERROR_HANDLER;
+    }
+
     if (virConnectGetConfigFile(&conf) < 0)
         goto failed;
 
-- 
1.7.11.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]