util: netdevip: use VIR_AUTOFREE instead of VIR_FREE for scalar types

By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls 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:27 +05:30 committed by Erik Skultety
parent a7a8b74928
commit 8214abd60e

View File

@ -171,11 +171,11 @@ virNetDevIPAddrAdd(const char *ifname,
virSocketAddr *broadcast = NULL; virSocketAddr *broadcast = NULL;
int ret = -1; int ret = -1;
struct nl_msg *nlmsg = NULL; struct nl_msg *nlmsg = NULL;
struct nlmsghdr *resp = NULL;
unsigned int recvbuflen; unsigned int recvbuflen;
char *ipStr = NULL; VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
char *peerStr = NULL; VIR_AUTOFREE(char *) ipStr = NULL;
char *bcastStr = NULL; VIR_AUTOFREE(char *) peerStr = NULL;
VIR_AUTOFREE(char *) bcastStr = NULL;
ipStr = virSocketAddrFormat(addr); ipStr = virSocketAddrFormat(addr);
if (peer && VIR_SOCKET_ADDR_VALID(peer)) if (peer && VIR_SOCKET_ADDR_VALID(peer))
@ -225,11 +225,7 @@ virNetDevIPAddrAdd(const char *ifname,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(ipStr);
VIR_FREE(peerStr);
VIR_FREE(bcastStr);
nlmsg_free(nlmsg); nlmsg_free(nlmsg);
VIR_FREE(resp);
VIR_FREE(broadcast); VIR_FREE(broadcast);
return ret; return ret;
} }
@ -252,8 +248,8 @@ virNetDevIPAddrDel(const char *ifname,
{ {
int ret = -1; int ret = -1;
struct nl_msg *nlmsg = NULL; struct nl_msg *nlmsg = NULL;
struct nlmsghdr *resp = NULL;
unsigned int recvbuflen; unsigned int recvbuflen;
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname, if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_DELADDR, ifname,
addr, prefix, addr, prefix,
@ -273,7 +269,6 @@ virNetDevIPAddrDel(const char *ifname,
ret = 0; ret = 0;
cleanup: cleanup:
nlmsg_free(nlmsg); nlmsg_free(nlmsg);
VIR_FREE(resp);
return ret; return ret;
} }
@ -309,8 +304,8 @@ virNetDevIPRouteAdd(const char *ifname,
int errCode; int errCode;
virSocketAddr defaultAddr; virSocketAddr defaultAddr;
virSocketAddrPtr actualAddr; virSocketAddrPtr actualAddr;
char *toStr = NULL; VIR_AUTOFREE(char *) toStr = NULL;
char *viaStr = NULL; VIR_AUTOFREE(char *) viaStr = NULL;
actualAddr = addr; actualAddr = addr;
@ -383,8 +378,6 @@ virNetDevIPRouteAdd(const char *ifname,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(toStr);
VIR_FREE(viaStr);
nlmsg_free(nlmsg); nlmsg_free(nlmsg);
return ret; return ret;
@ -452,7 +445,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
{ {
struct nl_msg *nlmsg = NULL; struct nl_msg *nlmsg = NULL;
struct ifaddrmsg ifa; struct ifaddrmsg ifa;
struct nlmsghdr *resp = NULL;
unsigned int recvbuflen; unsigned int recvbuflen;
int ret = -1; int ret = -1;
bool dad = true; bool dad = true;
@ -475,6 +467,8 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
/* Periodically query netlink until DAD finishes on all known addresses. */ /* Periodically query netlink until DAD finishes on all known addresses. */
while (dad && time(NULL) < max_time) { while (dad && time(NULL) < max_time) {
VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0, if (virNetlinkCommand(nlmsg, &resp, &recvbuflen, 0, 0,
NETLINK_ROUTE, 0) < 0) NETLINK_ROUTE, 0) < 0)
goto cleanup; goto cleanup;
@ -489,8 +483,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
dad = virNetDevIPParseDadStatus(resp, recvbuflen, addrs, count); dad = virNetDevIPParseDadStatus(resp, recvbuflen, addrs, count);
if (dad) if (dad)
usleep(1000 * 10); usleep(1000 * 10);
VIR_FREE(resp);
} }
/* Check timeout. */ /* Check timeout. */
if (dad) { if (dad) {
@ -502,7 +494,6 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
} }
cleanup: cleanup:
VIR_FREE(resp);
nlmsg_free(nlmsg); nlmsg_free(nlmsg);
return ret; return ret;
} }
@ -510,22 +501,18 @@ virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
static int static int
virNetDevIPGetAcceptRA(const char *ifname) virNetDevIPGetAcceptRA(const char *ifname)
{ {
char *path = NULL; VIR_AUTOFREE(char *) path = NULL;
char *buf = NULL; VIR_AUTOFREE(char *) buf = NULL;
char *suffix; char *suffix;
int accept_ra = -1; int accept_ra = -1;
if (virAsprintf(&path, "/proc/sys/net/ipv6/conf/%s/accept_ra", if (virAsprintf(&path, "/proc/sys/net/ipv6/conf/%s/accept_ra",
ifname ? ifname : "all") < 0) ifname ? ifname : "all") < 0)
goto cleanup; return -1;
if ((virFileReadAll(path, 512, &buf) < 0) || if ((virFileReadAll(path, 512, &buf) < 0) ||
(virStrToLong_i(buf, &suffix, 10, &accept_ra) < 0)) (virStrToLong_i(buf, &suffix, 10, &accept_ra) < 0))
goto cleanup; return -1;
cleanup:
VIR_FREE(path);
VIR_FREE(buf);
return accept_ra; return accept_ra;
} }
@ -545,17 +532,16 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct nlmsghdr *resp,
struct rtmsg *rtmsg = NLMSG_DATA(resp); struct rtmsg *rtmsg = NLMSG_DATA(resp);
int accept_ra = -1; int accept_ra = -1;
struct rtattr *rta; struct rtattr *rta;
char *ifname = NULL;
struct virNetDevIPCheckIPv6ForwardingData *data = opaque; struct virNetDevIPCheckIPv6ForwardingData *data = opaque;
int ret = 0;
int len = RTM_PAYLOAD(resp); int len = RTM_PAYLOAD(resp);
int oif = -1; int oif = -1;
size_t i; size_t i;
bool hasDevice; bool hasDevice;
VIR_AUTOFREE(char *) ifname = NULL;
/* Ignore messages other than route ones */ /* Ignore messages other than route ones */
if (resp->nlmsg_type != RTM_NEWROUTE) if (resp->nlmsg_type != RTM_NEWROUTE)
return ret; return 0;
/* Extract a device ID attribute */ /* Extract a device ID attribute */
VIR_WARNINGS_NO_CAST_ALIGN VIR_WARNINGS_NO_CAST_ALIGN
@ -566,21 +552,20 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct nlmsghdr *resp,
/* Should never happen: netlink message would be broken */ /* Should never happen: netlink message would be broken */
if (ifname) { if (ifname) {
char *ifname2 = virNetDevGetName(oif); VIR_AUTOFREE(char *) ifname2 = virNetDevGetName(oif);
VIR_WARN("Single route has unexpected 2nd interface " VIR_WARN("Single route has unexpected 2nd interface "
"- '%s' and '%s'", ifname, ifname2); "- '%s' and '%s'", ifname, ifname2);
VIR_FREE(ifname2);
break; break;
} }
if (!(ifname = virNetDevGetName(oif))) if (!(ifname = virNetDevGetName(oif)))
goto error; return -1;
} }
} }
/* No need to do anything else for non RA routes */ /* No need to do anything else for non RA routes */
if (rtmsg->rtm_protocol != RTPROT_RA) if (rtmsg->rtm_protocol != RTPROT_RA)
goto cleanup; return 0;
data->hasRARoutes = true; data->hasRARoutes = true;
@ -595,15 +580,9 @@ virNetDevIPCheckIPv6ForwardingCallback(const struct nlmsghdr *resp,
} }
if (accept_ra != 2 && !hasDevice && if (accept_ra != 2 && !hasDevice &&
VIR_APPEND_ELEMENT(data->devices, data->ndevices, ifname) < 0) VIR_APPEND_ELEMENT(data->devices, data->ndevices, ifname) < 0)
goto error; return -1;
cleanup: return 0;
VIR_FREE(ifname);
return ret;
error:
ret = -1;
goto cleanup;
} }
bool bool
@ -687,9 +666,11 @@ virNetDevIPAddrAdd(const char *ifname,
unsigned int prefix) unsigned int prefix)
{ {
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
char *addrstr = NULL, *bcaststr = NULL, *peerstr = NULL;
virSocketAddr broadcast; virSocketAddr broadcast;
int ret = -1; int ret = -1;
VIR_AUTOFREE(char *) addrstr = NULL;
VIR_AUTOFREE(char *) bcaststr = NULL;
VIR_AUTOFREE(char *) peerstr = NULL;
if (!(addrstr = virSocketAddrFormat(addr))) if (!(addrstr = virSocketAddrFormat(addr)))
goto cleanup; goto cleanup;
@ -733,9 +714,6 @@ virNetDevIPAddrAdd(const char *ifname,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(addrstr);
VIR_FREE(bcaststr);
VIR_FREE(peerstr);
virCommandFree(cmd); virCommandFree(cmd);
return ret; return ret;
} }
@ -747,8 +725,8 @@ virNetDevIPAddrDel(const char *ifname,
unsigned int prefix) unsigned int prefix)
{ {
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
char *addrstr;
int ret = -1; int ret = -1;
VIR_AUTOFREE(char *) addrstr = NULL;
if (!(addrstr = virSocketAddrFormat(addr))) if (!(addrstr = virSocketAddrFormat(addr)))
goto cleanup; goto cleanup;
@ -773,7 +751,6 @@ virNetDevIPAddrDel(const char *ifname,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(addrstr);
virCommandFree(cmd); virCommandFree(cmd);
return ret; return ret;
} }
@ -787,8 +764,9 @@ virNetDevIPRouteAdd(const char *ifname,
unsigned int metric) unsigned int metric)
{ {
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
char *addrstr = NULL, *gatewaystr = NULL;
int ret = -1; int ret = -1;
VIR_AUTOFREE(char *) addrstr = NULL;
VIR_AUTOFREE(char *) gatewaystr = NULL;
if (!(addrstr = virSocketAddrFormat(addr))) if (!(addrstr = virSocketAddrFormat(addr)))
goto cleanup; goto cleanup;
@ -806,8 +784,6 @@ virNetDevIPRouteAdd(const char *ifname,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(addrstr);
VIR_FREE(gatewaystr);
virCommandFree(cmd); virCommandFree(cmd);
return ret; return ret;
} }
@ -1083,9 +1059,9 @@ int
virNetDevIPInfoAddToDev(const char *ifname, virNetDevIPInfoAddToDev(const char *ifname,
virNetDevIPInfo const *ipInfo) virNetDevIPInfo const *ipInfo)
{ {
int ret = -1;
size_t i; size_t i;
int prefix; int prefix;
VIR_AUTOFREE(char *) ipStr = NULL;
/* add all IP addresses */ /* add all IP addresses */
for (i = 0; i < ipInfo->nips; i++) { for (i = 0; i < ipInfo->nips; i++) {
@ -1093,16 +1069,14 @@ virNetDevIPInfoAddToDev(const char *ifname,
if ((prefix = virSocketAddrGetIPPrefix(&ip->address, if ((prefix = virSocketAddrGetIPPrefix(&ip->address,
NULL, ip->prefix)) < 0) { NULL, ip->prefix)) < 0) {
char *ipStr = virSocketAddrFormat(&ip->address); ipStr = virSocketAddrFormat(&ip->address);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to determine prefix for IP address '%s'"), _("Failed to determine prefix for IP address '%s'"),
NULLSTR(ipStr)); NULLSTR(ipStr));
VIR_FREE(ipStr); return -1;
goto cleanup;
} }
if (virNetDevIPAddrAdd(ifname, &ip->address, &ip->peer, prefix) < 0) if (virNetDevIPAddrAdd(ifname, &ip->address, &ip->peer, prefix) < 0)
goto cleanup; return -1;
} }
/* add all routes */ /* add all routes */
@ -1110,23 +1084,19 @@ virNetDevIPInfoAddToDev(const char *ifname,
virNetDevIPRoutePtr route = ipInfo->routes[i]; virNetDevIPRoutePtr route = ipInfo->routes[i];
if ((prefix = virNetDevIPRouteGetPrefix(route)) < 0) { if ((prefix = virNetDevIPRouteGetPrefix(route)) < 0) {
char *ipStr = virSocketAddrFormat(&route->address); ipStr = virSocketAddrFormat(&route->address);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to determine prefix for route with destination '%s'"), _("Failed to determine prefix for route with destination '%s'"),
NULLSTR(ipStr)); NULLSTR(ipStr));
VIR_FREE(ipStr); return -1;
goto cleanup;
} }
if (virNetDevIPRouteAdd(ifname, &route->address, prefix, if (virNetDevIPRouteAdd(ifname, &route->address, prefix,
&route->gateway, &route->gateway,
virNetDevIPRouteGetMetric(route)) < 0) virNetDevIPRouteGetMetric(route)) < 0)
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
return ret;
} }
void void