util: iscsi: use VIR_AUTOPTR for aggregate types

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@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Sukrit Bhatnagar 2018-07-28 23:31:17 +05:30 committed by Erik Skultety
parent ec3e8789f1
commit 7ec89811a6

View File

@ -91,7 +91,7 @@ virISCSIGetSession(const char *devpath,
int exitstatus = 0; int exitstatus = 0;
VIR_AUTOFREE(char *) error = NULL; VIR_AUTOFREE(char *) error = NULL;
virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode", VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM, "--mode",
"session", NULL); "session", NULL);
virCommandSetErrorBuffer(cmd, &error); virCommandSetErrorBuffer(cmd, &error);
@ -101,15 +101,13 @@ virISCSIGetSession(const char *devpath,
vars, vars,
virISCSIExtractSession, virISCSIExtractSession,
&cbdata, NULL, &exitstatus) < 0) &cbdata, NULL, &exitstatus) < 0)
goto cleanup; return NULL;
if (cbdata.session == NULL && !probe) if (cbdata.session == NULL && !probe)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot find iscsiadm session: %s"), _("cannot find iscsiadm session: %s"),
NULLSTR(error)); NULLSTR(error));
cleanup:
virCommandFree(cmd);
return cbdata.session; return cbdata.session;
} }
@ -125,11 +123,11 @@ virStorageBackendIQNFound(const char *initiatoriqn,
{ {
int ret = IQN_ERROR; int ret = IQN_ERROR;
char *line = NULL; char *line = NULL;
virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
"--mode", "iface", NULL);
VIR_AUTOFREE(char *) outbuf = NULL; VIR_AUTOFREE(char *) outbuf = NULL;
VIR_AUTOFREE(char *) iface = NULL; VIR_AUTOFREE(char *) iface = NULL;
VIR_AUTOFREE(char *) iqn = NULL; VIR_AUTOFREE(char *) iqn = NULL;
VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
"--mode", "iface", NULL);
*ifacename = NULL; *ifacename = NULL;
@ -196,7 +194,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
if (ret == IQN_MISSING) if (ret == IQN_MISSING)
VIR_DEBUG("Could not find interface with IQN '%s'", iqn); VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
virCommandFree(cmd);
return ret; return ret;
error: error:
@ -212,8 +209,8 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
char **ifacename) char **ifacename)
{ {
int ret = -1, exitstatus = -1; int ret = -1, exitstatus = -1;
virCommandPtr cmd = NULL;
VIR_AUTOFREE(char *) temp_ifacename = NULL; VIR_AUTOFREE(char *) temp_ifacename = NULL;
VIR_AUTOPTR(virCommand) cmd = NULL;
if (virAsprintf(&temp_ifacename, if (virAsprintf(&temp_ifacename,
"libvirt-iface-%08llx", "libvirt-iface-%08llx",
@ -272,7 +269,6 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
ret = 0; ret = 0;
cleanup: cleanup:
virCommandFree(cmd);
if (ret != 0) if (ret != 0)
VIR_FREE(*ifacename); VIR_FREE(*ifacename);
return ret; return ret;
@ -285,7 +281,6 @@ virISCSIConnection(const char *portal,
const char *target, const char *target,
const char **extraargv) const char **extraargv)
{ {
int ret = -1;
const char *const baseargv[] = { const char *const baseargv[] = {
ISCSIADM, ISCSIADM,
"--mode", "node", "--mode", "node",
@ -293,7 +288,7 @@ virISCSIConnection(const char *portal,
"--targetname", target, "--targetname", target,
NULL NULL
}; };
virCommandPtr cmd; VIR_AUTOPTR(virCommand) cmd = NULL;
VIR_AUTOFREE(char *) ifacename = NULL; VIR_AUTOFREE(char *) ifacename = NULL;
cmd = virCommandNewArgs(baseargv); cmd = virCommandNewArgs(baseargv);
@ -306,7 +301,7 @@ virISCSIConnection(const char *portal,
break; break;
case IQN_MISSING: case IQN_MISSING:
if (virStorageBackendCreateIfaceIQN(initiatoriqn, &ifacename) != 0) if (virStorageBackendCreateIfaceIQN(initiatoriqn, &ifacename) != 0)
goto cleanup; return -1;
/* /*
* iscsiadm doesn't let you send commands to the Interface IQN, * iscsiadm doesn't let you send commands to the Interface IQN,
* unless you've first issued a 'sendtargets' command to the * unless you've first issued a 'sendtargets' command to the
@ -317,25 +312,20 @@ virISCSIConnection(const char *portal,
*/ */
if (virISCSIScanTargetsInternal(portal, ifacename, if (virISCSIScanTargetsInternal(portal, ifacename,
true, NULL, NULL) < 0) true, NULL, NULL) < 0)
goto cleanup; return -1;
break; break;
case IQN_ERROR: case IQN_ERROR:
default: default:
goto cleanup; return -1;
} }
virCommandAddArgList(cmd, "--interface", ifacename, NULL); virCommandAddArgList(cmd, "--interface", ifacename, NULL);
} }
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
@ -362,14 +352,12 @@ virISCSIConnectionLogout(const char *portal,
int int
virISCSIRescanLUNs(const char *session) virISCSIRescanLUNs(const char *session)
{ {
virCommandPtr cmd = virCommandNewArgList(ISCSIADM, VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
"--mode", "session", "--mode", "session",
"-r", session, "-r", session,
"-R", "-R",
NULL); NULL);
int ret = virCommandRun(cmd, NULL); return virCommandRun(cmd, NULL);
virCommandFree(cmd);
return ret;
} }
@ -419,8 +407,7 @@ virISCSIScanTargetsInternal(const char *portal,
int vars[] = { 2 }; int vars[] = { 2 };
struct virISCSITargetList list; struct virISCSITargetList list;
size_t i; size_t i;
int ret = -1; VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList(ISCSIADM,
virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
"--mode", "discovery", "--mode", "discovery",
"--type", "sendtargets", "--type", "sendtargets",
"--portal", portal, "--portal", portal,
@ -446,7 +433,7 @@ virISCSIScanTargetsInternal(const char *portal,
vars, vars,
virISCSIGetTargets, virISCSIGetTargets,
&list, NULL, NULL) < 0) &list, NULL, NULL) < 0)
goto cleanup; return -1;
if (ntargetsret && targetsret) { if (ntargetsret && targetsret) {
*ntargetsret = list.ntargets; *ntargetsret = list.ntargets;
@ -457,10 +444,7 @@ virISCSIScanTargetsInternal(const char *portal,
VIR_FREE(list.targets); VIR_FREE(list.targets);
} }
ret = 0; return 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
@ -538,9 +522,8 @@ int
virISCSINodeNew(const char *portal, virISCSINodeNew(const char *portal,
const char *target) const char *target)
{ {
virCommandPtr cmd = NULL; VIR_AUTOPTR(virCommand) cmd = NULL;
int status; int status;
int ret = -1;
cmd = virCommandNewArgList(ISCSIADM, cmd = virCommandNewArgList(ISCSIADM,
"--mode", "node", "--mode", "node",
@ -553,20 +536,17 @@ virISCSINodeNew(const char *portal,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed new node mode for target '%s'"), _("Failed new node mode for target '%s'"),
target); target);
goto cleanup; return -1;
} }
if (status != 0) { if (status != 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s failed new mode for target '%s' with status '%d'"), _("%s failed new mode for target '%s' with status '%d'"),
ISCSIADM, target, status); ISCSIADM, target, status);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
@ -576,9 +556,8 @@ virISCSINodeUpdate(const char *portal,
const char *name, const char *name,
const char *value) const char *value)
{ {
virCommandPtr cmd = NULL; VIR_AUTOPTR(virCommand) cmd = NULL;
int status; int status;
int ret = -1;
cmd = virCommandNewArgList(ISCSIADM, cmd = virCommandNewArgList(ISCSIADM,
"--mode", "node", "--mode", "node",
@ -594,11 +573,8 @@ virISCSINodeUpdate(const char *portal,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to update '%s' of node mode for target '%s'"), _("Failed to update '%s' of node mode for target '%s'"),
name, target); name, target);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
virCommandFree(cmd);
return ret;
} }