[PATCH 6/8] virsh: Add wrapper for virNodeDeviceFree

Michal Privoznik mprivozn at redhat.com
Mon Sep 27 05:07:48 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       |  2 +-
 tools/virsh-completer-nodedev.c |  3 +-
 tools/virsh-nodedev.c           | 49 ++++++++++-----------------------
 tools/virsh-util.c              | 11 ++++++++
 tools/virsh-util.h              |  5 ++++
 5 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk
index 0c0d844d6c..6c230826bd 100644
--- a/build-aux/syntax-check.mk
+++ b/build-aux/syntax-check.mk
@@ -868,7 +868,7 @@ sc_gettext_init:
 	  $(_sc_search_regexp)
 
 sc_prohibit_obj_free_apis_in_virsh:
-	@prohibit='\bvir(Domain|DomainSnapshot|Interface|Network|Secret|StoragePool|StorageVol)Free\b' \
+	@prohibit='\bvir(Domain|DomainSnapshot|Interface|Network|NodeDevice|Secret|StoragePool|StorageVol)Free\b' \
 	in_vc_files='virsh.*\.[ch]$$' \
 	exclude='sc_prohibit_obj_free_apis_in_virsh' \
 	halt='avoid using public virXXXFree in virsh, use virsh-prefixed wrappers instead' \
diff --git a/tools/virsh-completer-nodedev.c b/tools/virsh-completer-nodedev.c
index d595b687fd..d10bf2b78c 100644
--- a/tools/virsh-completer-nodedev.c
+++ b/tools/virsh-completer-nodedev.c
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #include "virsh-completer-nodedev.h"
+#include "virsh-util.h"
 #include "conf/node_device_conf.h"
 #include "viralloc.h"
 #include "virsh-nodedev.h"
@@ -58,7 +59,7 @@ virshNodeDeviceNameCompleter(vshControl *ctl,
     ret = g_steal_pointer(&tmp);
 
     for (i = 0; i < ndevs; i++)
-        virNodeDeviceFree(devs[i]);
+        virshNodeDeviceFree(devs[i]);
     g_free(devs);
     return ret;
 }
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index f72359121f..c989a77ad2 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -20,6 +20,7 @@
 
 #include <config.h>
 #include "virsh-nodedev.h"
+#include "virsh-util.h"
 
 #include "internal.h"
 #include "viralloc.h"
@@ -55,7 +56,7 @@ static const vshCmdOptDef opts_node_device_create[] = {
 static bool
 cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     const char *from = NULL;
     g_autofree char *buffer = NULL;
     virshControl *priv = ctl->privData;
@@ -73,7 +74,6 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
 
     vshPrintExtra(ctl, _("Node device %s created from %s\n"),
                   virNodeDeviceGetName(dev), from);
-    virNodeDeviceFree(dev);
     return true;
 }
 
@@ -140,7 +140,7 @@ vshFindNodeDevice(vshControl *ctl, const char *value)
 static bool
 cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     const char *device_value = NULL;
 
@@ -160,8 +160,6 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -207,8 +205,7 @@ virshNodeDeviceListFree(struct virshNodeDeviceList *list)
 
     if (list && list->devices) {
         for (i = 0; i < list->ndevices; i++) {
-            if (list->devices[i])
-                virNodeDeviceFree(list->devices[i]);
+            virshNodeDeviceFree(list->devices[i]);
         }
         g_free(list->devices);
     }
@@ -327,8 +324,7 @@ virshNodeDeviceListCollect(vshControl *ctl,
 
  remove_entry:
         /* the device has to be removed as it failed one of the filters */
-        virNodeDeviceFree(list->devices[i]);
-        list->devices[i] = NULL;
+        g_clear_pointer(&list->devices[i], virshNodeDeviceFree);
         deleted++;
     }
 
@@ -576,7 +572,7 @@ static const vshCmdOptDef opts_node_device_dumpxml[] = {
 static bool
 cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr device = NULL;
+    g_autoptr(virshNodeDevice) device = NULL;
     g_autofree char *xml = NULL;
     const char *device_value = NULL;
     bool ret = false;
@@ -596,8 +592,6 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (device)
-        virNodeDeviceFree(device);
     return ret;
 }
 
@@ -634,7 +628,7 @@ cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
     const char *driverName = NULL;
-    virNodeDevicePtr device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -664,7 +658,6 @@ cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
     else
         vshError(ctl, _("Failed to detach device %s"), name);
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -696,7 +689,7 @@ static bool
 cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
-    virNodeDevicePtr device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -715,7 +708,6 @@ cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
         ret = false;
     }
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -747,7 +739,7 @@ static bool
 cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
-    virNodeDevicePtr device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -766,7 +758,6 @@ cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
         ret = false;
     }
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -910,7 +901,7 @@ static const vshCmdOptDef opts_node_device_event[] = {
 static bool
 cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     int eventId = -1;
     int timeout = 0;
@@ -988,8 +979,6 @@ cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd)
     if (eventId >= 0 &&
         virConnectNodeDeviceEventDeregisterAny(priv->conn, eventId) < 0)
         ret = false;
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -1020,7 +1009,7 @@ static const vshCmdOptDef opts_node_device_undefine[] = {
 static bool
 cmdNodeDeviceUndefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     const char *device_value = NULL;
 
@@ -1041,8 +1030,6 @@ cmdNodeDeviceUndefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 
     ret = true;
  cleanup:
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -1071,7 +1058,7 @@ static const vshCmdOptDef opts_node_device_define[] = {
 static bool
 cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     const char *from = NULL;
     g_autofree char *buffer = NULL;
     virshControl *priv = ctl->privData;
@@ -1089,7 +1076,6 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 
     vshPrintExtra(ctl, _("Node device '%s' defined from '%s'\n"),
                   virNodeDeviceGetName(dev), from);
-    virNodeDeviceFree(dev);
     return true;
 }
 
@@ -1121,7 +1107,7 @@ static bool
 cmdNodeDeviceStart(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
-    virNodeDevice *device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -1140,7 +1126,6 @@ cmdNodeDeviceStart(vshControl *ctl, const vshCmd *cmd)
         ret = false;
     }
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -1175,7 +1160,7 @@ static const vshCmdOptDef opts_node_device_autostart[] = {
 static bool
 cmdNodeDeviceAutostart(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     const char *name = NULL;
     int autostart;
@@ -1204,8 +1189,6 @@ cmdNodeDeviceAutostart(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -1237,7 +1220,7 @@ static const vshCmdOptDef opts_node_device_info[] = {
 static bool
 cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr device = NULL;
+    g_autoptr(virshNodeDevice) device = NULL;
     const char *device_value = NULL;
     bool ret = false;
     int autostart;
@@ -1265,8 +1248,6 @@ cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (device)
-        virNodeDeviceFree(device);
     return ret;
 }
 
diff --git a/tools/virsh-util.c b/tools/virsh-util.c
index f7b649983e..5034f4773f 100644
--- a/tools/virsh-util.c
+++ b/tools/virsh-util.c
@@ -307,6 +307,17 @@ virshNetworkFree(virNetworkPtr network)
 }
 
 
+void
+virshNodeDeviceFree(virNodeDevicePtr device)
+{
+    if (!device)
+        return;
+
+    vshSaveLibvirtHelperError();
+    virNodeDeviceFree(device); /* sc_prohibit_obj_free_apis_in_virsh */
+}
+
+
 void
 virshSecretFree(virSecretPtr secret)
 {
diff --git a/tools/virsh-util.h b/tools/virsh-util.h
index e8df0a6618..06e311b21a 100644
--- a/tools/virsh-util.h
+++ b/tools/virsh-util.h
@@ -64,6 +64,11 @@ void
 virshNetworkFree(virNetworkPtr network);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNetwork, virshNetworkFree);
 
+typedef virNodeDevice virshNodeDevice;
+void
+virshNodeDeviceFree(virNodeDevicePtr device);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNodeDevice, virshNodeDeviceFree);
+
 typedef virSecret virshSecret;
 void
 virshSecretFree(virSecretPtr secret);
-- 
2.32.0




More information about the libvir-list mailing list