[libvirt] [PATCH v2 02/35] util: iscsi: use VIR_AUTOPTR for aggregate types

Sukrit Bhatnagar skrtbhtngr at gmail.com
Sun Aug 5 20:43:29 UTC 2018


By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr at gmail.com>
---
 src/util/viriscsi.c | 100 +++++++++++++++++++---------------------------------
 1 file changed, 36 insertions(+), 64 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index 0c2d8bb..8c272b5 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -89,10 +89,10 @@ virISCSIGetSession(const char *devpath,
         .devpath = devpath,
     };
     int exitstatus = 0;
+    VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM, "--mode",
+                                                       "session", NULL);
     VIR_AUTOFREE(char *) error = NULL;
 
-    virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
-                                             "session", NULL);
     virCommandSetErrorBuffer(cmd, &error);
 
     if (virCommandRunRegex(cmd,
@@ -101,15 +101,13 @@ virISCSIGetSession(const char *devpath,
                            vars,
                            virISCSIExtractSession,
                            &cbdata, NULL, &exitstatus) < 0)
-        goto cleanup;
+        return cbdata.session;
 
     if (cbdata.session == NULL && !probe)
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot find iscsiadm session: %s"),
                        NULLSTR(error));
 
- cleanup:
-    virCommandFree(cmd);
     return cbdata.session;
 }
 
@@ -125,8 +123,8 @@ virStorageBackendIQNFound(const char *initiatoriqn,
 {
     int ret = IQN_ERROR;
     char *line = NULL;
-    virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
-                                             "--mode", "iface", NULL);
+    VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
+                                                       "--mode", "iface", NULL);
     VIR_AUTOFREE(char *) outbuf = NULL;
     VIR_AUTOFREE(char *) iqn = NULL;
 
@@ -195,7 +193,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
     if (ret == IQN_MISSING)
         VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
 
-    virCommandFree(cmd);
     return ret;
 
  error:
@@ -210,8 +207,8 @@ static int
 virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
                                 char **ifacename)
 {
-    int ret = -1, exitstatus = -1;
-    virCommandPtr cmd = NULL;
+    int exitstatus = -1;
+    VIR_AUTOPTR(virCommand) cmd = NULL;
     VIR_AUTOFREE(char *) iface_name = NULL;
     VIR_AUTOFREE(char *) temp_ifacename = NULL;
 
@@ -236,7 +233,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to run command '%s' to create new iscsi interface"),
                        ISCSIADM);
-        goto cleanup;
+        return -1;
     }
     virCommandFree(cmd);
 
@@ -255,7 +252,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to run command '%s' to update iscsi interface with IQN '%s'"),
                        ISCSIADM, initiatoriqn);
-        goto cleanup;
+        return -1;
     }
 
     /* Check again to make sure the interface was created. */
@@ -263,7 +260,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
         VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
                   "after attempting to create it",
                   &temp_ifacename[0], initiatoriqn);
-        goto cleanup;
+        return -1;
     } else {
         VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
                   iface_name, initiatoriqn);
@@ -271,12 +268,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
 
     VIR_STEAL_PTR(*ifacename, iface_name);
 
-    virCommandFree(cmd);
     return 0;
-
- cleanup:
-    virCommandFree(cmd);
-    return ret;
 }
 
 
@@ -286,7 +278,6 @@ virISCSIConnection(const char *portal,
                    const char *target,
                    const char **extraargv)
 {
-    int ret = -1;
     const char *const baseargv[] = {
         ISCSIADM,
         "--mode", "node",
@@ -294,7 +285,7 @@ virISCSIConnection(const char *portal,
         "--targetname", target,
         NULL
     };
-    virCommandPtr cmd;
+    VIR_AUTOPTR(virCommand) cmd = NULL;
     VIR_AUTOFREE(char *) ifacename = NULL;
 
     cmd = virCommandNewArgs(baseargv);
@@ -307,7 +298,7 @@ virISCSIConnection(const char *portal,
             break;
         case IQN_MISSING:
             if (virStorageBackendCreateIfaceIQN(initiatoriqn, &ifacename) != 0)
-                goto cleanup;
+                return -1;
             /*
              * iscsiadm doesn't let you send commands to the Interface IQN,
              * unless you've first issued a 'sendtargets' command to the
@@ -318,25 +309,20 @@ virISCSIConnection(const char *portal,
              */
             if (virISCSIScanTargetsInternal(portal, ifacename,
                                             true, NULL, NULL) < 0)
-                goto cleanup;
+                return -1;
 
             break;
         case IQN_ERROR:
         default:
-            goto cleanup;
+            return -1;
         }
         virCommandAddArgList(cmd, "--interface", ifacename, NULL);
     }
 
     if (virCommandRun(cmd, NULL) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = 0;
-
- cleanup:
-    virCommandFree(cmd);
-
-    return ret;
+    return 0;
 }
 
 
@@ -363,14 +349,12 @@ virISCSIConnectionLogout(const char *portal,
 int
 virISCSIRescanLUNs(const char *session)
 {
-    virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
-                                             "--mode", "session",
-                                             "-r", session,
-                                             "-R",
-                                             NULL);
-    int ret = virCommandRun(cmd, NULL);
-    virCommandFree(cmd);
-    return ret;
+    VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
+                                                       "--mode", "session",
+                                                       "-r", session,
+                                                       "-R",
+                                                       NULL);
+    return virCommandRun(cmd, NULL);
 }
 
 
@@ -420,12 +404,11 @@ virISCSIScanTargetsInternal(const char *portal,
     int vars[] = { 2 };
     struct virISCSITargetList list;
     size_t i;
-    int ret = -1;
-    virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
-                                             "--mode", "discovery",
-                                             "--type", "sendtargets",
-                                             "--portal", portal,
-                                             NULL);
+    VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
+                                                       "--mode", "discovery",
+                                                       "--type", "sendtargets",
+                                                       "--portal", portal,
+                                                       NULL);
 
     if (!persist) {
         virCommandAddArgList(cmd,
@@ -447,7 +430,7 @@ virISCSIScanTargetsInternal(const char *portal,
                            vars,
                            virISCSIGetTargets,
                            &list, NULL, NULL) < 0)
-        goto cleanup;
+        return -1;
 
     if (ntargetsret && targetsret) {
         *ntargetsret = list.ntargets;
@@ -458,10 +441,7 @@ virISCSIScanTargetsInternal(const char *portal,
         VIR_FREE(list.targets);
     }
 
-    ret = 0;
- cleanup:
-    virCommandFree(cmd);
-    return ret;
+    return 0;
 }
 
 
@@ -539,9 +519,8 @@ int
 virISCSINodeNew(const char *portal,
                 const char *target)
 {
-    virCommandPtr cmd = NULL;
     int status;
-    int ret = -1;
+    VIR_AUTOPTR(virCommand) cmd = NULL;
 
     cmd = virCommandNewArgList(ISCSIADM,
                                "--mode", "node",
@@ -554,20 +533,17 @@ virISCSINodeNew(const char *portal,
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed new node mode for target '%s'"),
                        target);
-        goto cleanup;
+        return -1;
     }
 
     if (status != 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("%s failed new mode for target '%s' with status '%d'"),
                        ISCSIADM, target, status);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
- cleanup:
-    virCommandFree(cmd);
-    return ret;
+    return 0;
 }
 
 
@@ -577,9 +553,8 @@ virISCSINodeUpdate(const char *portal,
                    const char *name,
                    const char *value)
 {
-    virCommandPtr cmd = NULL;
     int status;
-    int ret = -1;
+    VIR_AUTOPTR(virCommand) cmd = NULL;
 
     cmd = virCommandNewArgList(ISCSIADM,
                                "--mode", "node",
@@ -595,11 +570,8 @@ virISCSINodeUpdate(const char *portal,
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to update '%s' of node mode for target '%s'"),
                        name, target);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
- cleanup:
-    virCommandFree(cmd);
-    return ret;
+    return 0;
 }
-- 
1.8.3.1




More information about the libvir-list mailing list