[PATCH v2 1/9] virsh: Work around virSecretFree quirks

Peter Krempa pkrempa at redhat.com
Fri Jan 24 16:08:33 UTC 2020


Similarly to other libvirt object freeing APIs the function resets the
libvirt error when called and doesn't take NULL gracefully. Install the
workaround and g_autoptr handlers similarly to the 'virshDomain' type.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 build-aux/syntax-check.mk      |  2 +-
 tools/virsh-completer-secret.c |  3 ++-
 tools/virsh-secret.c           | 22 ++++++++++------------
 tools/virsh-util.c             | 11 +++++++++++
 tools/virsh-util.h             |  5 +++++
 5 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk
index e5623606dd..14122476de 100644
--- a/build-aux/syntax-check.mk
+++ b/build-aux/syntax-check.mk
@@ -1118,7 +1118,7 @@ sc_gettext_init:
 	  $(_sc_search_regexp)

 sc_prohibit_obj_free_apis_in_virsh:
-	@prohibit='\bvir(Domain|DomainSnapshot)Free\b' \
+	@prohibit='\bvir(Domain|DomainSnapshot|Secret)Free\b' \
 	in_vc_files='virsh.*\.[ch]$$' \
 	exclude='sc_prohibit_obj_free_apis_in_virsh' \
 	halt='avoid using virDomain(Snapshot)Free in virsh, use virsh-prefixed wrappers instead' \
diff --git a/tools/virsh-completer-secret.c b/tools/virsh-completer-secret.c
index a90fdb399f..505b19db84 100644
--- a/tools/virsh-completer-secret.c
+++ b/tools/virsh-completer-secret.c
@@ -23,6 +23,7 @@
 #include "virsh-completer-secret.h"
 #include "viralloc.h"
 #include "virsh-secret.h"
+#include "virsh-util.h"
 #include "virsh.h"
 #include "virstring.h"

@@ -61,7 +62,7 @@ virshSecretUUIDCompleter(vshControl *ctl,

  cleanup:
     for (i = 0; i < nsecrets; i++)
-        virSecretFree(secrets[i]);
+        virshSecretFree(secrets[i]);
     VIR_FREE(secrets);
     return ret;
 }
diff --git a/tools/virsh-secret.c b/tools/virsh-secret.c
index 01c62b9ce8..7123b10860 100644
--- a/tools/virsh-secret.c
+++ b/tools/virsh-secret.c
@@ -20,6 +20,7 @@

 #include <config.h>
 #include "virsh-secret.h"
+#include "virsh-util.h"

 #include "internal.h"
 #include "virbuffer.h"
@@ -106,8 +107,7 @@ cmdSecretDefine(vshControl *ctl, const vshCmd *cmd)

  cleanup:
     VIR_FREE(buffer);
-    if (res)
-        virSecretFree(res);
+    virshSecretFree(res);
     return ret;
 }

@@ -153,7 +153,7 @@ cmdSecretDumpXML(vshControl *ctl, const vshCmd *cmd)
     ret = true;

  cleanup:
-    virSecretFree(secret);
+    virshSecretFree(secret);
     return ret;
 }

@@ -215,7 +215,7 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
     ret = true;

  cleanup:
-    virSecretFree(secret);
+    virshSecretFree(secret);
     return ret;
 }

@@ -266,7 +266,7 @@ cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)

  cleanup:
     VIR_DISPOSE_N(value, value_size);
-    virSecretFree(secret);
+    virshSecretFree(secret);
     return ret;
 }

@@ -312,7 +312,7 @@ cmdSecretUndefine(vshControl *ctl, const vshCmd *cmd)
     ret = true;

  cleanup:
-    virSecretFree(secret);
+    virshSecretFree(secret);
     return ret;
 }

@@ -348,10 +348,9 @@ virshSecretListFree(virshSecretListPtr list)
     size_t i;

     if (list && list->secrets) {
-        for (i = 0; i < list->nsecrets; i++) {
-            if (list->secrets[i])
-                virSecretFree(list->secrets[i]);
-        }
+        for (i = 0; i < list->nsecrets; i++)
+            virshSecretFree(list->secrets[i]);
+
         VIR_FREE(list->secrets);
     }
     VIR_FREE(list);
@@ -763,8 +762,7 @@ cmdSecretEvent(vshControl *ctl, const vshCmd *cmd)
     if (eventId >= 0 &&
         virConnectSecretEventDeregisterAny(priv->conn, eventId) < 0)
         ret = false;
-    if (secret)
-        virSecretFree(secret);
+    virshSecretFree(secret);
     return ret;
 }

diff --git a/tools/virsh-util.c b/tools/virsh-util.c
index f436fbb486..932d6d0849 100644
--- a/tools/virsh-util.c
+++ b/tools/virsh-util.c
@@ -250,6 +250,17 @@ virshDomainSnapshotFree(virDomainSnapshotPtr snap)
 }


+void
+virshSecretFree(virSecretPtr secret)
+{
+    if (!secret)
+        return;
+
+    vshSaveLibvirtHelperError();
+    virSecretFree(secret); /* sc_prohibit_obj_free_apis_in_virsh */
+}
+
+
 int
 virshDomainGetXMLFromDom(vshControl *ctl,
                          virDomainPtr dom,
diff --git a/tools/virsh-util.h b/tools/virsh-util.h
index 2b0447268e..72653d9735 100644
--- a/tools/virsh-util.h
+++ b/tools/virsh-util.h
@@ -45,6 +45,11 @@ void
 virshDomainFree(virDomainPtr dom);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshDomain, virshDomainFree);

+typedef virSecret virshSecret;
+void
+virshSecretFree(virSecretPtr secret);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshSecret, virshSecretFree);
+
 void
 virshDomainCheckpointFree(virDomainCheckpointPtr chk);

-- 
2.24.1




More information about the libvir-list mailing list