mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06: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:
parent
b16e623b27
commit
5392743c10
@ -403,7 +403,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
|
|||||||
.ifi_index = ifindex
|
.ifi_index = ifindex
|
||||||
};
|
};
|
||||||
unsigned int recvbuflen;
|
unsigned int recvbuflen;
|
||||||
struct nl_msg *nl_msg;
|
VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
|
||||||
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
|
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
|
||||||
|
|
||||||
if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
|
if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
|
||||||
@ -442,7 +442,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
|
|||||||
|
|
||||||
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
|
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
|
||||||
src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
|
src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
||||||
goto malformed_resp;
|
goto malformed_resp;
|
||||||
@ -457,7 +457,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
|
|||||||
virReportSystemError(-err->error,
|
virReportSystemError(-err->error,
|
||||||
_("error dumping %s (%d) interface"),
|
_("error dumping %s (%d) interface"),
|
||||||
ifname, ifindex);
|
ifname, ifindex);
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -474,21 +474,17 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
|
|||||||
}
|
}
|
||||||
|
|
||||||
VIR_STEAL_PTR(*nlData, resp);
|
VIR_STEAL_PTR(*nlData, resp);
|
||||||
rc = 0;
|
return 0;
|
||||||
|
|
||||||
cleanup:
|
|
||||||
nlmsg_free(nl_msg);
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
malformed_resp:
|
malformed_resp:
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("malformed netlink response message"));
|
_("malformed netlink response message"));
|
||||||
goto cleanup;
|
return rc;
|
||||||
|
|
||||||
buffer_too_small:
|
buffer_too_small:
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("allocated netlink buffer is too small"));
|
_("allocated netlink buffer is too small"));
|
||||||
goto cleanup;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -510,11 +506,10 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
|
|||||||
int
|
int
|
||||||
virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
|
virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
|
||||||
{
|
{
|
||||||
int rc = -1;
|
|
||||||
struct nlmsgerr *err;
|
struct nlmsgerr *err;
|
||||||
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
|
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
|
||||||
unsigned int recvbuflen;
|
unsigned int recvbuflen;
|
||||||
struct nl_msg *nl_msg;
|
VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
|
||||||
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
|
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
|
||||||
|
|
||||||
nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
|
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,
|
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0,
|
||||||
NETLINK_ROUTE, 0) < 0) {
|
NETLINK_ROUTE, 0) < 0) {
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
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)))
|
if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
|
||||||
goto malformed_resp;
|
goto malformed_resp;
|
||||||
|
|
||||||
if (-err->error == EOPNOTSUPP && fallback) {
|
if (-err->error == EOPNOTSUPP && fallback)
|
||||||
rc = fallback(ifname);
|
return fallback(ifname);
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (err->error) {
|
if (err->error) {
|
||||||
virReportSystemError(-err->error,
|
virReportSystemError(-err->error,
|
||||||
_("error destroying network device %s"),
|
_("error destroying network device %s"),
|
||||||
ifname);
|
ifname);
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -563,20 +557,17 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
|
|||||||
goto malformed_resp;
|
goto malformed_resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = 0;
|
return 0;
|
||||||
cleanup:
|
|
||||||
nlmsg_free(nl_msg);
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
malformed_resp:
|
malformed_resp:
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("malformed netlink response message"));
|
_("malformed netlink response message"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
buffer_too_small:
|
buffer_too_small:
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("allocated netlink buffer is too small"));
|
_("allocated netlink buffer is too small"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -597,13 +588,12 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
|
|||||||
int
|
int
|
||||||
virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
|
virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
|
||||||
{
|
{
|
||||||
int rc = -1;
|
|
||||||
struct nlmsgerr *err;
|
struct nlmsgerr *err;
|
||||||
struct ndmsg ndinfo = {
|
struct ndmsg ndinfo = {
|
||||||
.ndm_family = AF_UNSPEC,
|
.ndm_family = AF_UNSPEC,
|
||||||
};
|
};
|
||||||
unsigned int recvbuflen;
|
unsigned int recvbuflen;
|
||||||
struct nl_msg *nl_msg;
|
VIR_AUTOPTR(virNetlinkMsg) nl_msg = NULL;
|
||||||
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
|
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
|
||||||
|
|
||||||
nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
|
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,
|
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
|
||||||
src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
|
src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
||||||
goto malformed_resp;
|
goto malformed_resp;
|
||||||
@ -632,7 +622,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
|
|||||||
if (err->error) {
|
if (err->error) {
|
||||||
virReportSystemError(-err->error,
|
virReportSystemError(-err->error,
|
||||||
"%s", _("error dumping"));
|
"%s", _("error dumping"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -644,21 +634,17 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
VIR_STEAL_PTR(*nlData, resp);
|
VIR_STEAL_PTR(*nlData, resp);
|
||||||
rc = recvbuflen;
|
return recvbuflen;
|
||||||
|
|
||||||
cleanup:
|
|
||||||
nlmsg_free(nl_msg);
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
malformed_resp:
|
malformed_resp:
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("malformed netlink response message"));
|
_("malformed netlink response message"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
buffer_too_small:
|
buffer_too_small:
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("allocated netlink buffer is too small"));
|
_("allocated netlink buffer is too small"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
Loading…
Reference in New Issue
Block a user