1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-26 03:21:44 +03:00

util: netlink: 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-08-09 09:42:12 +05:30 committed by Erik Skultety
parent b16e623b27
commit 5392743c10

View File

@ -403,7 +403,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
.ifi_index = ifindex
};
unsigned int recvbuflen;
struct nl_msg *nl_msg;
VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
@ -442,7 +442,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
goto cleanup;
return -1;
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
goto malformed_resp;
@ -457,7 +457,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
virReportSystemError(-err->error,
_("error dumping %s (%d) interface"),
ifname, ifindex);
goto cleanup;
return -1;
}
break;
@ -474,21 +474,17 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
}
VIR_STEAL_PTR(*nlData, resp);
rc = 0;
cleanup:
nlmsg_free(nl_msg);
return rc;
return 0;
malformed_resp:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
goto cleanup;
return rc;
buffer_too_small:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
goto cleanup;
return rc;
}
@ -510,11 +506,10 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
int
virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
{
int rc = -1;
struct nlmsgerr *err;
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
unsigned int recvbuflen;
struct nl_msg *nl_msg;
VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
@ -532,7 +527,7 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0,
NETLINK_ROUTE, 0) < 0) {
goto cleanup;
return -1;
}
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
@ -544,15 +539,14 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
goto malformed_resp;
if (-err->error == EOPNOTSUPP && fallback) {
rc = fallback(ifname);
goto cleanup;
}
if (-err->error == EOPNOTSUPP && fallback)
return fallback(ifname);
if (err->error) {
virReportSystemError(-err->error,
_("error destroying network device %s"),
ifname);
goto cleanup;
return -1;
}
break;
@ -563,20 +557,17 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
goto malformed_resp;
}
rc = 0;
cleanup:
nlmsg_free(nl_msg);
return rc;
return 0;
malformed_resp:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
goto cleanup;
return -1;
buffer_too_small:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
goto cleanup;
return -1;
}
/**
@ -597,13 +588,12 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
int
virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
{
int rc = -1;
struct nlmsgerr *err;
struct ndmsg ndinfo = {
.ndm_family = AF_UNSPEC,
};
unsigned int recvbuflen;
struct nl_msg *nl_msg;
VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
@ -618,7 +608,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
goto cleanup;
return -1;
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
goto malformed_resp;
@ -632,7 +622,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
if (err->error) {
virReportSystemError(-err->error,
"%s", _("error dumping"));
goto cleanup;
return -1;
}
break;
@ -644,21 +634,17 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
}
VIR_STEAL_PTR(*nlData, resp);
rc = recvbuflen;
cleanup:
nlmsg_free(nl_msg);
return rc;
return recvbuflen;
malformed_resp:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed netlink response message"));
goto cleanup;
return -1;
buffer_too_small:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("allocated netlink buffer is too small"));
goto cleanup;
return -1;
}
int