add-domain: Suppress libvirt errors on stderr.
authorRichard W.M. Jones <rjones@redhat.com>
Fri, 6 May 2011 16:32:59 +0000 (12:32 -0400)
committerRichard W.M. Jones <rjones@redhat.com>
Fri, 6 May 2011 17:03:50 +0000 (13:03 -0400)
Install an error handler on the libvirt error connection so that
errors are not printed on stderr (instead they go up through the usual
libguestfs error mechanism).

Unfortunately this doesn't suppress initial connection error messages
to stderr.  I cannot see how to do this without affecting the global
libvirt error handler, which is not acceptable for a library to be
doing.

src/virt.c

index aa2c022..cd48888 100644 (file)
@@ -53,6 +53,12 @@ init_libxml2 (void)
   LIBXML_TEST_VERSION;
 }
 
   LIBXML_TEST_VERSION;
 }
 
+static void
+ignore_errors (void *ignore, virErrorPtr ignore2)
+{
+  /* empty */
+}
+
 struct guestfs___add_libvirt_dom_argv {
   uint64_t bitmask;
 #define GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK (UINT64_C(1)<<0)
 struct guestfs___add_libvirt_dom_argv {
   uint64_t bitmask;
 #define GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK (UINT64_C(1)<<0)
@@ -102,6 +108,12 @@ guestfs__add_domain (guestfs_h *g, const char *domain_name,
     goto cleanup;
   }
 
     goto cleanup;
   }
 
+  /* Suppress default behaviour of printing errors to stderr.  Note
+   * you can't set this to NULL to ignore errors; setting it to NULL
+   * restores the default error handler ...
+   */
+  virConnSetErrorFunc (conn, NULL, ignore_errors);
+
   dom = virDomainLookupByName (conn, domain_name);
   if (!dom) {
     err = virGetLastError ();
   dom = virDomainLookupByName (conn, domain_name);
   if (!dom) {
     err = virGetLastError ();