[PATCH] storage: Don't overwrite error in virISCSIDirectDisconnect()

Michal Privoznik posted 1 patch 2 years, 10 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/89397c4277937f8c71af965e021ac5e7c4f4fe26.1622623550.git.mprivozn@redhat.com
src/storage/storage_backend_iscsi_direct.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
[PATCH] storage: Don't overwrite error in virISCSIDirectDisconnect()
Posted by Michal Privoznik 2 years, 10 months ago
The iscsi-direct storage pool backend works merely like this: a
connection is established to the target (usually done via
virStorageBackendISCSIDirectSetConnection()), intended action is
executed (e.g. reporting LUNs, volume wiping), and at the end the
connection is closed via virISCSIDirectDisconnect().

The problem is that virISCSIDirectDisconnect() reports its own
errors which may overwrite error that occurred during LUN
reporting, or volume wiping or whatever.

To fix this, use virErrorPreserveLast() + virErrorRestore()
combo, which either preserves previously reported error message,
or is NOP if there's no error reported.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1797879
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/storage/storage_backend_iscsi_direct.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/storage/storage_backend_iscsi_direct.c b/src/storage/storage_backend_iscsi_direct.c
index 0bff1882b9..e4a14c3fd6 100644
--- a/src/storage/storage_backend_iscsi_direct.c
+++ b/src/storage/storage_backend_iscsi_direct.c
@@ -391,19 +391,28 @@ virISCSIDirectReportLuns(virStoragePoolObj *pool,
 static int
 virISCSIDirectDisconnect(struct iscsi_context *iscsi)
 {
+    virErrorPtr orig_err;
+    int ret = -1;
+
+    virErrorPreserveLast(&orig_err);
+
     if (iscsi_logout_sync(iscsi) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to logout: %s"),
                        iscsi_get_error(iscsi));
-        return -1;
+        goto cleanup;
     }
     if (iscsi_disconnect(iscsi) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to disconnect: %s"),
                        iscsi_get_error(iscsi));
-        return -1;
+        goto cleanup;
     }
-    return 0;
+
+    ret = 0;
+ cleanup:
+    virErrorRestore(&orig_err);
+    return ret;
 }
 
 static int
-- 
2.31.1

Re: [PATCH] storage: Don't overwrite error in virISCSIDirectDisconnect()
Posted by Jano Tomko 2 years, 10 months ago
On 6/2/21 10:46 AM, Michal Privoznik wrote:
> The iscsi-direct storage pool backend works merely like this: a
> connection is established to the target (usually done via
> virStorageBackendISCSIDirectSetConnection()), intended action is
> executed (e.g. reporting LUNs, volume wiping), and at the end the
> connection is closed via virISCSIDirectDisconnect().
> 
> The problem is that virISCSIDirectDisconnect() reports its own
> errors which may overwrite error that occurred during LUN
> reporting, or volume wiping or whatever.
> 
> To fix this, use virErrorPreserveLast() + virErrorRestore()
> combo, which either preserves previously reported error message,
> or is NOP if there's no error reported.
> 
> Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1797879
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/storage/storage_backend_iscsi_direct.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano