提交 85e04482 编写于 作者: S Sukrit Bhatnagar 提交者: Erik Skultety

util: netdevip: 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: NSukrit Bhatnagar <skrtbhtngr@gmail.com>
Reviewed-by: NErik Skultety <eskultet@redhat.com>
上级 5266bb91
...@@ -168,10 +168,9 @@ virNetDevIPAddrAdd(const char *ifname, ...@@ -168,10 +168,9 @@ virNetDevIPAddrAdd(const char *ifname,
virSocketAddr *peer, virSocketAddr *peer,
unsigned int prefix) unsigned int prefix)
{ {
virSocketAddr *broadcast = NULL;
int ret = -1;
struct nl_msg *nlmsg = NULL;
unsigned int recvbuflen; unsigned int recvbuflen;
VIR_AUTOPTR(virNetlinkMsg) nlmsg = NULL;
VIR_AUTOPTR(virSocketAddr) broadcast = NULL;
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL; VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
VIR_AUTOFREE(char *) ipStr = NULL; VIR_AUTOFREE(char *) ipStr = NULL;
VIR_AUTOFREE(char *) peerStr = NULL; VIR_AUTOFREE(char *) peerStr = NULL;
...@@ -186,13 +185,13 @@ virNetDevIPAddrAdd(const char *ifname, ...@@ -186,13 +185,13 @@ virNetDevIPAddrAdd(const char *ifname,
!(peer && VIR_SOCKET_ADDR_VALID(peer))) { !(peer && VIR_SOCKET_ADDR_VALID(peer))) {
/* compute a broadcast address if this is IPv4 */ /* compute a broadcast address if this is IPv4 */
if (VIR_ALLOC(broadcast) < 0) if (VIR_ALLOC(broadcast) < 0)
goto cleanup; return -1;
if (virSocketAddrBroadcastByPrefix(addr, prefix, broadcast) < 0) { if (virSocketAddrBroadcastByPrefix(addr, prefix, broadcast) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to determine broadcast address for '%s/%d'"), _("Failed to determine broadcast address for '%s/%d'"),
ipStr, prefix); ipStr, prefix);
goto cleanup; return -1;
} }
bcastStr = virSocketAddrFormat(broadcast); bcastStr = virSocketAddrFormat(broadcast);
} }
...@@ -206,11 +205,11 @@ virNetDevIPAddrAdd(const char *ifname, ...@@ -206,11 +205,11 @@ virNetDevIPAddrAdd(const char *ifname,
if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_NEWADDR, ifname, if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_NEWADDR, ifname,
addr, prefix, addr, prefix,
broadcast, peer))) broadcast, peer)))
goto cleanup; return -1;
if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, if (virNetlinkCommand(nlmsg, &resp, &recvbuflen,
0, 0, NETLINK_ROUTE, 0) < 0) 0, 0, NETLINK_ROUTE, 0) < 0)
goto cleanup; return -1;
if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) { if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) {
...@@ -220,14 +219,10 @@ virNetDevIPAddrAdd(const char *ifname, ...@@ -220,14 +219,10 @@ virNetDevIPAddrAdd(const char *ifname,
peerStr ? " peer " : "", peerStr ? peerStr : "", peerStr ? " peer " : "", peerStr ? peerStr : "",
bcastStr ? " bcast " : "", bcastStr ? bcastStr : "", bcastStr ? " bcast " : "", bcastStr ? bcastStr : "",
ifname); ifname);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
nlmsg_free(nlmsg);
VIR_FREE(broadcast);
return ret;
} }
...@@ -246,30 +241,26 @@ virNetDevIPAddrDel(const char *ifname, ...@@ -246,30 +241,26 @@ virNetDevIPAddrDel(const char *ifname,
virSocketAddr *addr, virSocketAddr *addr,
unsigned int prefix) unsigned int prefix)
{ {
int ret = -1;
struct nl_msg *nlmsg = NULL;
unsigned int recvbuflen; unsigned int recvbuflen;
VIR_AUTOPTR(virNetlinkMsg) nlmsg = NULL;
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL; VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname, if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname,
addr, prefix, addr, prefix,
NULL, NULL))) NULL, NULL)))
goto cleanup; return -1;
if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0, if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0,
NETLINK_ROUTE, 0) < 0) NETLINK_ROUTE, 0) < 0)
goto cleanup; return -1;
if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) { if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR, virReportError(VIR_ERR_SYSTEM_ERROR,
_("Error removing IP address from %s"), ifname); _("Error removing IP address from %s"), ifname);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
nlmsg_free(nlmsg);
return ret;
} }
...@@ -292,8 +283,6 @@ virNetDevIPRouteAdd(const char *ifname, ...@@ -292,8 +283,6 @@ virNetDevIPRouteAdd(const char *ifname,
virSocketAddrPtr gateway, virSocketAddrPtr gateway,
unsigned int metric) unsigned int metric)
{ {
int ret = -1;
struct nl_msg *nlmsg = NULL;
struct nlmsghdr *resp = NULL; struct nlmsghdr *resp = NULL;
unsigned int recvbuflen; unsigned int recvbuflen;
unsigned int ifindex; unsigned int ifindex;
...@@ -304,6 +293,7 @@ virNetDevIPRouteAdd(const char *ifname, ...@@ -304,6 +293,7 @@ virNetDevIPRouteAdd(const char *ifname,
int errCode; int errCode;
virSocketAddr defaultAddr; virSocketAddr defaultAddr;
virSocketAddrPtr actualAddr; virSocketAddrPtr actualAddr;
VIR_AUTOPTR(virNetlinkMsg) nlmsg = NULL;
VIR_AUTOFREE(char *) toStr = NULL; VIR_AUTOFREE(char *) toStr = NULL;
VIR_AUTOFREE(char *) viaStr = NULL; VIR_AUTOFREE(char *) viaStr = NULL;
...@@ -315,10 +305,10 @@ virNetDevIPRouteAdd(const char *ifname, ...@@ -315,10 +305,10 @@ virNetDevIPRouteAdd(const char *ifname,
int family = VIR_SOCKET_ADDR_FAMILY(gateway); int family = VIR_SOCKET_ADDR_FAMILY(gateway);
if (family == AF_INET) { if (family == AF_INET) {
if (virSocketAddrParseIPv4(&defaultAddr, VIR_SOCKET_ADDR_IPV4_ALL) < 0) if (virSocketAddrParseIPv4(&defaultAddr, VIR_SOCKET_ADDR_IPV4_ALL) < 0)
goto cleanup; return -1;
} else { } else {
if (virSocketAddrParseIPv6(&defaultAddr, VIR_SOCKET_ADDR_IPV6_ALL) < 0) if (virSocketAddrParseIPv6(&defaultAddr, VIR_SOCKET_ADDR_IPV6_ALL) < 0)
goto cleanup; return -1;
} }
actualAddr = &defaultAddr; actualAddr = &defaultAddr;
...@@ -330,17 +320,17 @@ virNetDevIPRouteAdd(const char *ifname, ...@@ -330,17 +320,17 @@ virNetDevIPRouteAdd(const char *ifname,
if (virNetDevGetIPAddressBinary(actualAddr, &addrData, &addrDataLen) < 0 || if (virNetDevGetIPAddressBinary(actualAddr, &addrData, &addrDataLen) < 0 ||
virNetDevGetIPAddressBinary(gateway, &gatewayData, &addrDataLen) < 0) virNetDevGetIPAddressBinary(gateway, &gatewayData, &addrDataLen) < 0)
goto cleanup; return -1;
/* Get the interface index */ /* Get the interface index */
if ((ifindex = if_nametoindex(ifname)) == 0) if ((ifindex = if_nametoindex(ifname)) == 0)
goto cleanup; return -1;
if (!(nlmsg = nlmsg_alloc_simple(RTM_NEWROUTE, if (!(nlmsg = nlmsg_alloc_simple(RTM_NEWROUTE,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REQUEST | NLM_F_CREATE |
NLM_F_EXCL))) { NLM_F_EXCL))) {
virReportOOMError(); virReportOOMError();
goto cleanup; return -1;
} }
memset(&rtmsg, 0, sizeof(rtmsg)); memset(&rtmsg, 0, sizeof(rtmsg));
...@@ -369,22 +359,19 @@ virNetDevIPRouteAdd(const char *ifname, ...@@ -369,22 +359,19 @@ virNetDevIPRouteAdd(const char *ifname,
if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0, if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0,
NETLINK_ROUTE, 0) < 0) NETLINK_ROUTE, 0) < 0)
goto cleanup; return -1;
if ((errCode = virNetlinkGetErrorCode(resp, recvbuflen)) < 0) { if ((errCode = virNetlinkGetErrorCode(resp, recvbuflen)) < 0) {
virReportSystemError(errCode, _("Error adding route to %s"), ifname); virReportSystemError(errCode, _("Error adding route to %s"), ifname);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
nlmsg_free(nlmsg);
return ret;
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;
} }
...@@ -443,12 +430,11 @@ virNetDevIPParseDadStatus(struct nlmsghdr *nlh, int len, ...@@ -443,12 +430,11 @@ virNetDevIPParseDadStatus(struct nlmsghdr *nlh, int len,
int int
virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count) virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
{ {
struct nl_msg *nlmsg = NULL;
struct ifaddrmsg ifa; struct ifaddrmsg ifa;
unsigned int recvbuflen; unsigned int recvbuflen;
int ret = -1;
bool dad = true; bool dad = true;
time_t max_time = time(NULL) + VIR_DAD_WAIT_TIMEOUT; time_t max_time = time(NULL) + VIR_DAD_WAIT_TIMEOUT;
VIR_AUTOPTR(virNetlinkMsg) nlmsg = NULL;
if (!(nlmsg = nlmsg_alloc_simple(RTM_GETADDR, if (!(nlmsg = nlmsg_alloc_simple(RTM_GETADDR,
NLM_F_REQUEST | NLM_F_DUMP))) { NLM_F_REQUEST | NLM_F_DUMP))) {
...@@ -462,7 +448,7 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count) ...@@ -462,7 +448,7 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
if (nlmsg_append(nlmsg, &ifa, sizeof(ifa), NLMSG_ALIGNTO) < 0) { if (nlmsg_append(nlmsg, &ifa, sizeof(ifa), NLMSG_ALIGNTO) < 0) {
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;
} }
/* Periodically query netlink until DAD finishes on all known addresses. */ /* Periodically query netlink until DAD finishes on all known addresses. */
...@@ -471,12 +457,12 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count) ...@@ -471,12 +457,12 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0, if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0,
NETLINK_ROUTE, 0) < 0) NETLINK_ROUTE, 0) < 0)
goto cleanup; return -1;
if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) { if (virNetlinkGetErrorCode(resp, recvbuflen) < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR, "%s", virReportError(VIR_ERR_SYSTEM_ERROR, "%s",
_("error reading DAD state information")); _("error reading DAD state information"));
goto cleanup; return -1;
} }
/* Parse response. */ /* Parse response. */
...@@ -490,12 +476,10 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count) ...@@ -490,12 +476,10 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
_("Duplicate Address Detection " _("Duplicate Address Detection "
"not finished in %d seconds"), VIR_DAD_WAIT_TIMEOUT); "not finished in %d seconds"), VIR_DAD_WAIT_TIMEOUT);
} else { } else {
ret = 0; return 0;
} }
cleanup: return -1;
nlmsg_free(nlmsg);
return ret;
} }
static int static int
...@@ -588,7 +572,6 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct nlmsghdr *resp, ...@@ -588,7 +572,6 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct nlmsghdr *resp,
bool bool
virNetDevIPCheckIPv6Forwarding(void) virNetDevIPCheckIPv6Forwarding(void)
{ {
struct nl_msg *nlmsg = NULL;
bool valid = false; bool valid = false;
struct rtgenmsg genmsg; struct rtgenmsg genmsg;
size_t i; size_t i;
...@@ -597,6 +580,7 @@ virNetDevIPCheckIPv6Forwarding(void) ...@@ -597,6 +580,7 @@ virNetDevIPCheckIPv6Forwarding(void)
.devices = NULL, .devices = NULL,
.ndevices = 0 .ndevices = 0
}; };
VIR_AUTOPTR(virNetlinkMsg) nlmsg = NULL;
/* Prepare the request message */ /* Prepare the request message */
...@@ -650,7 +634,6 @@ virNetDevIPCheckIPv6Forwarding(void) ...@@ -650,7 +634,6 @@ virNetDevIPCheckIPv6Forwarding(void)
} }
cleanup: cleanup:
nlmsg_free(nlmsg);
virStringListFreeCount(data.devices, data.ndevices); virStringListFreeCount(data.devices, data.ndevices);
return valid; return valid;
} }
...@@ -664,24 +647,23 @@ virNetDevIPAddrAdd(const char *ifname, ...@@ -664,24 +647,23 @@ virNetDevIPAddrAdd(const char *ifname,
virSocketAddr *peer, virSocketAddr *peer,
unsigned int prefix) unsigned int prefix)
{ {
virCommandPtr cmd = NULL;
virSocketAddr broadcast; virSocketAddr broadcast;
int ret = -1; VIR_AUTOPTR(virCommand) cmd = NULL;
VIR_AUTOFREE(char *) addrstr = NULL; VIR_AUTOFREE(char *) addrstr = NULL;
VIR_AUTOFREE(char *) bcaststr = NULL; VIR_AUTOFREE(char *) bcaststr = NULL;
VIR_AUTOFREE(char *) peerstr = NULL; VIR_AUTOFREE(char *) peerstr = NULL;
if (!(addrstr = virSocketAddrFormat(addr))) if (!(addrstr = virSocketAddrFormat(addr)))
goto cleanup; return -1;
if (peer && VIR_SOCKET_ADDR_VALID(peer) && !(peerstr = virSocketAddrFormat(peer))) if (peer && VIR_SOCKET_ADDR_VALID(peer) && !(peerstr = virSocketAddrFormat(peer)))
goto cleanup; return -1;
/* format up a broadcast address if this is IPv4 */ /* format up a broadcast address if this is IPv4 */
if (!peerstr && ((VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET)) && if (!peerstr && ((VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET)) &&
((virSocketAddrBroadcastByPrefix(addr, prefix, &broadcast) < 0) || ((virSocketAddrBroadcastByPrefix(addr, prefix, &broadcast) < 0) ||
!(bcaststr = virSocketAddrFormat(&broadcast))))) { !(bcaststr = virSocketAddrFormat(&broadcast))))) {
goto cleanup; return -1;
} }
# ifdef IFCONFIG_PATH # ifdef IFCONFIG_PATH
...@@ -709,12 +691,9 @@ virNetDevIPAddrAdd(const char *ifname, ...@@ -709,12 +691,9 @@ virNetDevIPAddrAdd(const char *ifname,
# endif # endif
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
...@@ -723,12 +702,11 @@ virNetDevIPAddrDel(const char *ifname, ...@@ -723,12 +702,11 @@ virNetDevIPAddrDel(const char *ifname,
virSocketAddr *addr, virSocketAddr *addr,
unsigned int prefix) unsigned int prefix)
{ {
virCommandPtr cmd = NULL; VIR_AUTOPTR(virCommand) cmd = NULL;
int ret = -1;
VIR_AUTOFREE(char *) addrstr = NULL; VIR_AUTOFREE(char *) addrstr = NULL;
if (!(addrstr = virSocketAddrFormat(addr))) if (!(addrstr = virSocketAddrFormat(addr)))
goto cleanup; return -1;
# ifdef IFCONFIG_PATH # ifdef IFCONFIG_PATH
cmd = virCommandNew(IFCONFIG_PATH); cmd = virCommandNew(IFCONFIG_PATH);
virCommandAddArg(cmd, ifname); virCommandAddArg(cmd, ifname);
...@@ -746,12 +724,9 @@ virNetDevIPAddrDel(const char *ifname, ...@@ -746,12 +724,9 @@ virNetDevIPAddrDel(const char *ifname,
# endif # endif
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
...@@ -762,15 +737,14 @@ virNetDevIPRouteAdd(const char *ifname, ...@@ -762,15 +737,14 @@ virNetDevIPRouteAdd(const char *ifname,
virSocketAddrPtr gateway, virSocketAddrPtr gateway,
unsigned int metric) unsigned int metric)
{ {
virCommandPtr cmd = NULL; VIR_AUTOPTR(virCommand) cmd = NULL;
int ret = -1;
VIR_AUTOFREE(char *) addrstr = NULL; VIR_AUTOFREE(char *) addrstr = NULL;
VIR_AUTOFREE(char *) gatewaystr = NULL; VIR_AUTOFREE(char *) gatewaystr = NULL;
if (!(addrstr = virSocketAddrFormat(addr))) if (!(addrstr = virSocketAddrFormat(addr)))
goto cleanup; return -1;
if (!(gatewaystr = virSocketAddrFormat(gateway))) if (!(gatewaystr = virSocketAddrFormat(gateway)))
goto cleanup; return -1;
cmd = virCommandNew(IP_PATH); cmd = virCommandNew(IP_PATH);
virCommandAddArgList(cmd, "route", "add", NULL); virCommandAddArgList(cmd, "route", "add", NULL);
virCommandAddArgFormat(cmd, "%s/%u", addrstr, prefix); virCommandAddArgFormat(cmd, "%s/%u", addrstr, prefix);
...@@ -779,12 +753,9 @@ virNetDevIPRouteAdd(const char *ifname, ...@@ -779,12 +753,9 @@ virNetDevIPRouteAdd(const char *ifname,
virCommandAddArgFormat(cmd, "%u", metric); virCommandAddArgFormat(cmd, "%u", metric);
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册