[PATCH 2/8] virsh: Add wrapper for virInterfaceFree

Michal Privoznik mprivozn at redhat.com
Mon Sep 27 05:07:44 UTC 2021


Similarly to virshDomainFree add a wrapper for the snapshot object
freeing function.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 build-aux/syntax-check.mk         |  4 +--
 tools/virsh-completer-interface.c |  3 +-
 tools/virsh-interface.c           | 55 +++++++++----------------------
 tools/virsh-util.c                | 11 +++++++
 tools/virsh-util.h                |  5 +++
 5 files changed, 36 insertions(+), 42 deletions(-)

diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk
index cb54c8ba36..84cb895d86 100644
--- a/build-aux/syntax-check.mk
+++ b/build-aux/syntax-check.mk
@@ -868,10 +868,10 @@ sc_gettext_init:
 	  $(_sc_search_regexp)
 
 sc_prohibit_obj_free_apis_in_virsh:
-	@prohibit='\bvir(Domain|DomainSnapshot|Secret)Free\b' \
+	@prohibit='\bvir(Domain|DomainSnapshot|Interface|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' \
+	halt='avoid using public virXXXFree in virsh, use virsh-prefixed wrappers instead' \
 	  $(_sc_search_regexp)
 
 https_sites = www.libvirt.org
diff --git a/tools/virsh-completer-interface.c b/tools/virsh-completer-interface.c
index 9a6603b27e..44ba35550a 100644
--- a/tools/virsh-completer-interface.c
+++ b/tools/virsh-completer-interface.c
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #include "virsh-completer-interface.h"
+#include "virsh-util.h"
 #include "viralloc.h"
 #include "virsh.h"
 #include "virstring.h"
@@ -59,7 +60,7 @@ virshInterfaceStringHelper(vshControl *ctl,
     }
 
     for (i = 0; i < nifaces; i++)
-        virInterfaceFree(ifaces[i]);
+        virshInterfaceFree(ifaces[i]);
     g_free(ifaces);
 
     return g_steal_pointer(&tmp);
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index f402119b68..0a8539da71 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -29,6 +29,7 @@
 
 #include <config.h>
 #include "virsh-interface.h"
+#include "virsh-util.h"
 
 #include <libxml/parser.h>
 #include <libxml/xpath.h>
@@ -109,8 +110,8 @@ static bool
 cmdInterfaceEdit(vshControl *ctl, const vshCmd *cmd)
 {
     bool ret = false;
-    virInterfacePtr iface = NULL;
-    virInterfacePtr iface_edited = NULL;
+    g_autoptr(virshInterface) iface = NULL;
+    g_autoptr(virshInterface) iface_edited = NULL;
     unsigned int flags = VIR_INTERFACE_XML_INACTIVE;
     virshControl *priv = ctl->privData;
 
@@ -136,11 +137,6 @@ cmdInterfaceEdit(vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
  cleanup:
-    if (iface)
-        virInterfaceFree(iface);
-    if (iface_edited)
-        virInterfaceFree(iface_edited);
-
     return ret;
 }
 
@@ -172,8 +168,7 @@ virshInterfaceListFree(struct virshInterfaceList *list)
 
     if (list && list->ifaces) {
         for (i = 0; i < list->nifaces; i++) {
-            if (list->ifaces[i])
-                virInterfaceFree(list->ifaces[i]);
+            virshInterfaceFree(list->ifaces[i]);
         }
         g_free(list->ifaces);
     }
@@ -411,14 +406,13 @@ static const vshCmdOptDef opts_interface_name[] = {
 static bool
 cmdInterfaceName(vshControl *ctl, const vshCmd *cmd)
 {
-    virInterfacePtr iface;
+    g_autoptr(virshInterface) iface = NULL;
 
     if (!(iface = virshCommandOptInterfaceBy(ctl, cmd, NULL, NULL,
                                              VIRSH_BYMAC)))
         return false;
 
     vshPrint(ctl, "%s\n", virInterfaceGetName(iface));
-    virInterfaceFree(iface);
     return true;
 }
 
@@ -448,14 +442,13 @@ static const vshCmdOptDef opts_interface_mac[] = {
 static bool
 cmdInterfaceMAC(vshControl *ctl, const vshCmd *cmd)
 {
-    virInterfacePtr iface;
+    g_autoptr(virshInterface) iface = NULL;
 
     if (!(iface = virshCommandOptInterfaceBy(ctl, cmd, NULL, NULL,
                                              VIRSH_BYNAME)))
         return false;
 
     vshPrint(ctl, "%s\n", virInterfaceGetMACString(iface));
-    virInterfaceFree(iface);
     return true;
 }
 
@@ -484,7 +477,7 @@ static const vshCmdOptDef opts_interface_dumpxml[] = {
 static bool
 cmdInterfaceDumpXML(vshControl *ctl, const vshCmd *cmd)
 {
-    virInterfacePtr iface;
+    g_autoptr(virshInterface) iface = NULL;
     g_autofree char *dump = NULL;
     unsigned int flags = 0;
 
@@ -494,13 +487,10 @@ cmdInterfaceDumpXML(vshControl *ctl, const vshCmd *cmd)
     if (!(iface = virshCommandOptInterface(ctl, cmd, NULL)))
         return false;
 
-    if (!(dump = virInterfaceGetXMLDesc(iface, flags))) {
-        virInterfaceFree(iface);
+    if (!(dump = virInterfaceGetXMLDesc(iface, flags)))
         return false;
-    }
 
     vshPrint(ctl, "%s", dump);
-    virInterfaceFree(iface);
     return true;
 }
 
@@ -530,7 +520,7 @@ static const vshCmdOptDef opts_interface_define[] = {
 static bool
 cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd)
 {
-    virInterfacePtr iface;
+    g_autoptr(virshInterface) iface = NULL;
     const char *from = NULL;
     g_autofree char *buffer = NULL;
     unsigned int flags = 0;
@@ -552,7 +542,6 @@ cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd)
 
     vshPrintExtra(ctl, _("Interface %s defined from %s\n"),
                   virInterfaceGetName(iface), from);
-    virInterfaceFree(iface);
     return true;
 }
 
@@ -577,7 +566,7 @@ static const vshCmdOptDef opts_interface_undefine[] = {
 static bool
 cmdInterfaceUndefine(vshControl *ctl, const vshCmd *cmd)
 {
-    virInterfacePtr iface;
+    g_autoptr(virshInterface) iface = NULL;
     const char *name;
 
     if (!(iface = virshCommandOptInterface(ctl, cmd, &name)))
@@ -585,12 +574,10 @@ cmdInterfaceUndefine(vshControl *ctl, const vshCmd *cmd)
 
     if (virInterfaceUndefine(iface) < 0) {
         vshError(ctl, _("Failed to undefine interface %s"), name);
-        virInterfaceFree(iface);
         return false;
     }
 
     vshPrintExtra(ctl, _("Interface %s undefined\n"), name);
-    virInterfaceFree(iface);
     return true;
 }
 
@@ -615,7 +602,7 @@ static const vshCmdOptDef opts_interface_start[] = {
 static bool
 cmdInterfaceStart(vshControl *ctl, const vshCmd *cmd)
 {
-    virInterfacePtr iface;
+    g_autoptr(virshInterface) iface = NULL;
     const char *name;
 
     if (!(iface = virshCommandOptInterface(ctl, cmd, &name)))
@@ -623,12 +610,10 @@ cmdInterfaceStart(vshControl *ctl, const vshCmd *cmd)
 
     if (virInterfaceCreate(iface, 0) < 0) {
         vshError(ctl, _("Failed to start interface %s"), name);
-        virInterfaceFree(iface);
         return false;
     }
 
     vshPrintExtra(ctl, _("Interface %s started\n"), name);
-    virInterfaceFree(iface);
     return true;
 }
 
@@ -653,7 +638,7 @@ static const vshCmdOptDef opts_interface_destroy[] = {
 static bool
 cmdInterfaceDestroy(vshControl *ctl, const vshCmd *cmd)
 {
-    virInterfacePtr iface;
+    g_autoptr(virshInterface) iface = NULL;
     const char *name;
 
     if (!(iface = virshCommandOptInterface(ctl, cmd, &name)))
@@ -661,12 +646,10 @@ cmdInterfaceDestroy(vshControl *ctl, const vshCmd *cmd)
 
     if (virInterfaceDestroy(iface, 0) < 0) {
         vshError(ctl, _("Failed to destroy interface %s"), name);
-        virInterfaceFree(iface);
         return false;
     }
 
     vshPrintExtra(ctl, _("Interface %s destroyed\n"), name);
-    virInterfaceFree(iface);
     return false;
 }
 
@@ -809,7 +792,8 @@ static bool
 cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
 {
     bool ret = false;
-    virInterfacePtr if_handle = NULL, br_handle = NULL;
+    g_autoptr(virshInterface) if_handle = NULL;
+    g_autoptr(virshInterface) br_handle = NULL;
     const char *if_name, *br_name;
     char *if_type = NULL, *if2_name = NULL, *delay_str = NULL;
     bool stp = false, nostart = false;
@@ -988,10 +972,6 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (if_handle)
-       virInterfaceFree(if_handle);
-    if (br_handle)
-       virInterfaceFree(br_handle);
     VIR_FREE(if_xml);
     VIR_FREE(br_xml);
     VIR_FREE(if_type);
@@ -1030,7 +1010,8 @@ static bool
 cmdInterfaceUnbridge(vshControl *ctl, const vshCmd *cmd)
 {
     bool ret = false;
-    virInterfacePtr if_handle = NULL, br_handle = NULL;
+    g_autoptr(virshInterface) if_handle = NULL;
+    g_autoptr(virshInterface) br_handle = NULL;
     const char *br_name;
     char *if_type = NULL, *if_name = NULL;
     bool nostart = false;
@@ -1187,10 +1168,6 @@ cmdInterfaceUnbridge(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (if_handle)
-       virInterfaceFree(if_handle);
-    if (br_handle)
-       virInterfaceFree(br_handle);
     VIR_FREE(if_xml);
     VIR_FREE(br_xml);
     VIR_FREE(if_type);
diff --git a/tools/virsh-util.c b/tools/virsh-util.c
index fb74514b3c..82523f2575 100644
--- a/tools/virsh-util.c
+++ b/tools/virsh-util.c
@@ -285,6 +285,17 @@ virshDomainSnapshotFree(virDomainSnapshotPtr snap)
 }
 
 
+void
+virshInterfaceFree(virInterfacePtr iface)
+{
+    if (!iface)
+        return;
+
+    vshSaveLibvirtHelperError();
+    virInterfaceFree(iface); /* sc_prohibit_obj_free_apis_in_virsh */
+}
+
+
 void
 virshSecretFree(virSecretPtr secret)
 {
diff --git a/tools/virsh-util.h b/tools/virsh-util.h
index f7e8116de3..7165755550 100644
--- a/tools/virsh-util.h
+++ b/tools/virsh-util.h
@@ -54,6 +54,11 @@ void
 virshDomainSnapshotFree(virDomainSnapshotPtr snap);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshDomainSnapshot, virshDomainSnapshotFree);
 
+typedef virInterface virshInterface;
+void
+virshInterfaceFree(virInterfacePtr iface);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshInterface, virshInterfaceFree);
+
 typedef virSecret virshSecret;
 void
 virshSecretFree(virSecretPtr secret);
-- 
2.32.0




More information about the libvir-list mailing list