[libvirt] [PATCH 5/9] vsh: Add helper for safe remembering of libvirt errors

Peter Krempa pkrempa at redhat.com
Tue Apr 11 15:53:51 UTC 2017


Avoid the annoying issue where the public object freeing APIs overwrite
the error set by helper functions, since they don't invoke the callback.

The new helper remembers the error only if no previous error was set.
---
 tools/virsh-util.c |  2 ++
 tools/vsh.c        | 15 +++++++++++++++
 tools/vsh.h        |  1 +
 3 files changed, 18 insertions(+)

diff --git a/tools/virsh-util.c b/tools/virsh-util.c
index e225d3332..79a38bb23 100644
--- a/tools/virsh-util.c
+++ b/tools/virsh-util.c
@@ -158,6 +158,7 @@ virshDomainFree(virDomainPtr dom)
     if (!dom)
         return;

+    vshSaveLibvirtHelperError();
     virDomainFree(dom); /* sc_prohibit_obj_free_apis_in_virsh */
 }

@@ -168,5 +169,6 @@ virshDomainSnapshotFree(virDomainSnapshotPtr snap)
     if (!snap)
         return;

+    vshSaveLibvirtHelperError();
     virDomainSnapshotFree(snap); /* sc_prohibit_obj_free_apis_in_virsh */
 }
diff --git a/tools/vsh.c b/tools/vsh.c
index d2024be91..10a65c39f 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -258,6 +258,21 @@ vshSaveLibvirtError(void)
     last_error = virSaveLastError();
 }

+
+/* Store libvirt error from helper API but don't overwrite existing errors */
+void
+vshSaveLibvirtHelperError(void)
+{
+    if (last_error)
+        return;
+
+    if (!virGetLastError())
+        return;
+
+    vshSaveLibvirtError();
+}
+
+
 /*
  * Reset libvirt error on graceful fallback paths
  */
diff --git a/tools/vsh.h b/tools/vsh.h
index 8f5d1a69f..2f686eba6 100644
--- a/tools/vsh.h
+++ b/tools/vsh.h
@@ -341,6 +341,7 @@ void vshErrorHandler(void *opaque, virErrorPtr error);
 void vshReportError(vshControl *ctl);
 void vshResetLibvirtError(void);
 void vshSaveLibvirtError(void);
+void vshSaveLibvirtHelperError(void);

 /* file handling */
 char *vshEditWriteToTempFile(vshControl *ctl, const char *doc);
-- 
2.12.2




More information about the libvir-list mailing list