[libvirt] [PATCH 1/3] util: error: Add helpers for saving and restoring of last error

Peter Krempa pkrempa at redhat.com
Tue Sep 12 10:09:44 UTC 2017


Some cleanup paths overwrite a usefull error message with a less useful
one and we then try to preserve the original message. The handlers added
in this patch will simplify the operations since they are designed right
for the purpose.
---
 src/libvirt_private.syms |  2 ++
 src/util/virerror.c      | 45 +++++++++++++++++++++++++++++++++++++++++++++
 src/util/virerror.h      |  3 +++
 3 files changed, 50 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f30a04b14..62e05186d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1604,6 +1604,8 @@ ebtablesRemoveForwardAllowIn;
 virDispatchError;
 virErrorCopyNew;
 virErrorInitialize;
+virErrorPreserveLast;
+virErrorRestore;
 virErrorSetErrnoFromLastError;
 virLastErrorIsSystemErrno;
 virRaiseErrorFull;
diff --git a/src/util/virerror.c b/src/util/virerror.c
index a5a2d6ed1..1f15c5dbb 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -371,6 +371,51 @@ virSaveLastError(void)
     return to;
 }

+
+/**
+ * virErrorPreserveLast:
+ * @saveerr: pointer to virErrorPtr for storing last error object
+ *
+ * Preserves the currently set last error (for the thread) into @saveerr so that
+ * it can be restored via virErrorRestore(). @saveerr must be passed to
+ * virErrorRestore()
+ */
+void
+virErrorPreserveLast(virErrorPtr *saveerr)
+{
+    int saved_errno = errno;
+    virErrorPtr lasterr = virGetLastError();
+
+    *saveerr = NULL;
+
+    if (lasterr)
+        *saveerr = virErrorCopyNew(lasterr);
+
+    errno = saved_errno;
+}
+
+
+/**
+ * virErrorRestore:
+ * @savederr: error object holding saved error
+ *
+ * Restores the error passed via @savederr and clears associated memory.
+ */
+void
+virErrorRestore(virErrorPtr *savederr)
+{
+    int saved_errno = errno;
+
+    if (!*savederr)
+        return;
+
+    virSetError(*savederr);
+    virFreeError(*savederr);
+    *savederr = NULL;
+    errno = saved_errno;
+}
+
+
 /**
  * virResetError:
  * @err: pointer to the virError to clean up
diff --git a/src/util/virerror.h b/src/util/virerror.h
index 234864812..54530d081 100644
--- a/src/util/virerror.h
+++ b/src/util/virerror.h
@@ -196,4 +196,7 @@ void virErrorSetErrnoFromLastError(void);

 bool virLastErrorIsSystemErrno(int errnum);

+void virErrorPreserveLast(virErrorPtr *saveerr);
+void virErrorRestore(virErrorPtr *savederr);
+
 #endif
-- 
2.14.1




More information about the libvir-list mailing list