By making use of GNU C's cleanup attribute handled by the VIR_AUTOCLOSE macro,
many of the VIR_FORCE_CLOSE calls can be dropped, which in turn leads to
getting rid of many of our cleanup sections.
Signed-off-by: Shi Lei <shi_lei(a)massclouds.com>
---
src/util/virnetdevbridge.c | 120 ++++++++++++-------------------------
1 file changed, 37 insertions(+), 83 deletions(-)
diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index bc377b5..d8528e1 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -81,9 +81,8 @@ static int virNetDevBridgeCmd(const char *brname,
void *arg,
size_t argsize)
{
- int s;
- int ret = -1;
struct ifdrv ifd;
+ VIR_AUTOCLOSE(s);
memset(&ifd, 0, sizeof(ifd));
@@ -97,19 +96,14 @@ static int virNetDevBridgeCmd(const char *brname,
virReportSystemError(ERANGE,
_("Network interface name '%s' is too
long"),
brname);
- goto cleanup;
+ return -1;
}
ifd.ifd_cmd = op;
ifd.ifd_len = argsize;
ifd.ifd_data = arg;
- ret = ioctl(s, SIOCSDRVSPEC, &ifd);
-
- cleanup:
- VIR_FORCE_CLOSE(s);
-
- return ret;
+ return ioctl(s, SIOCSDRVSPEC, &ifd);
}
#endif
@@ -167,10 +161,9 @@ static int virNetDevBridgeGet(const char *brname,
const char *paramname, /* sysfs param name */
unsigned long *value) /* current value */
{
- int ret = -1;
- int fd = -1;
struct ifreq ifr;
VIR_AUTOFREE(char *) path = NULL;
+ VIR_AUTOCLOSE(fd);
if (virAsprintf(&path, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname)
< 0)
return -1;
@@ -180,26 +173,26 @@ static int virNetDevBridgeGet(const char *brname,
if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long),
&valuestr) < 0)
- goto cleanup;
+ return -1;
if (virStrToLong_ul(valuestr, NULL, 10, value) < 0) {
virReportSystemError(EINVAL,
_("Unable to get bridge %s %s"),
brname, paramname);
- goto cleanup;
+ return -1;
}
} else {
struct __bridge_info info;
unsigned long args[] = { BRCTL_GET_BRIDGE_INFO, (unsigned long)&info, 0, 0
};
if ((fd = virNetDevSetupControl(brname, &ifr)) < 0)
- goto cleanup;
+ return -1;
ifr.ifr_data = (char*)&args;
if (ioctl(fd, SIOCDEVPRIVATE, ifr) < 0) {
virReportSystemError(errno,
_("Unable to get bridge %s %s"), brname,
paramname);
- goto cleanup;
+ return -1;
}
if (STREQ(paramname, "stp_state")) {
@@ -209,14 +202,11 @@ static int virNetDevBridgeGet(const char *brname,
} else {
virReportSystemError(EINVAL,
_("Unable to get bridge %s %s"), brname,
paramname);
- goto cleanup;
+ return -1;
}
}
- ret = 0;
- cleanup:
- VIR_FORCE_CLOSE(fd);
- return ret;
+ return 0;
}
#endif /* __linux__ */
@@ -391,8 +381,7 @@ virNetDevBridgePortSetUnicastFlood(const char *brname
ATTRIBUTE_UNUSED,
static int
virNetDevBridgeCreateWithIoctl(const char *brname)
{
- int fd = -1;
- int ret = -1;
+ VIR_AUTOCLOSE(fd);
if ((fd = virNetDevSetupControl(NULL, NULL)) < 0)
return -1;
@@ -400,14 +389,10 @@ virNetDevBridgeCreateWithIoctl(const char *brname)
if (ioctl(fd, SIOCBRADDBR, brname) < 0) {
virReportSystemError(errno,
_("Unable to create bridge %s"), brname);
- goto cleanup;
+ return -1;
}
- ret = 0;
-
- cleanup:
- VIR_FORCE_CLOSE(fd);
- return ret;
+ return 0;
}
#endif
@@ -503,9 +488,8 @@ virNetDevBridgeCreate(const char *brname)
int
virNetDevBridgeCreate(const char *brname)
{
- int s;
struct ifreq ifr;
- int ret = - 1;
+ VIR_AUTOCLOSE(s);
if ((s = virNetDevSetupControl("bridge", &ifr)) < 0)
return -1;
@@ -513,16 +497,13 @@ virNetDevBridgeCreate(const char *brname)
if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) {
virReportSystemError(errno, "%s",
_("Unable to create bridge device"));
- goto cleanup;
+ return -1;
}
if (virNetDevSetName(ifr.ifr_name, brname) == -1)
- goto cleanup;
+ return -1;
- ret = 0;
- cleanup:
- VIR_FORCE_CLOSE(s);
- return ret;
+ return 0;
}
#else
int virNetDevBridgeCreate(const char *brname)
@@ -545,8 +526,7 @@ int virNetDevBridgeCreate(const char *brname)
static int
virNetDevBridgeDeleteWithIoctl(const char *brname)
{
- int fd = -1;
- int ret = -1;
+ VIR_AUTOCLOSE(fd);
ignore_value(virNetDevSetOnline(brname, false));
@@ -556,14 +536,10 @@ virNetDevBridgeDeleteWithIoctl(const char *brname)
if (ioctl(fd, SIOCBRDELBR, brname) < 0) {
virReportSystemError(errno,
_("Unable to delete bridge %s"), brname);
- goto cleanup;
+ return -1;
}
- ret = 0;
-
- cleanup:
- VIR_FORCE_CLOSE(fd);
- return ret;
+ return 0;
}
#endif
@@ -596,9 +572,8 @@ virNetDevBridgeDelete(const char *brname)
int
virNetDevBridgeDelete(const char *brname)
{
- int s;
struct ifreq ifr;
- int ret = -1;
+ VIR_AUTOCLOSE(s);
if ((s = virNetDevSetupControl(brname, &ifr)) < 0)
return -1;
@@ -607,13 +582,10 @@ virNetDevBridgeDelete(const char *brname)
virReportSystemError(errno,
_("Unable to remove bridge %s"),
brname);
- goto cleanup;
+ return -1;
}
- ret = 0;
- cleanup:
- VIR_FORCE_CLOSE(s);
- return ret;
+ return 0;
}
#else
int virNetDevBridgeDelete(const char *brname ATTRIBUTE_UNUSED)
@@ -637,9 +609,8 @@ int virNetDevBridgeDelete(const char *brname ATTRIBUTE_UNUSED)
int virNetDevBridgeAddPort(const char *brname,
const char *ifname)
{
- int fd = -1;
- int ret = -1;
struct ifreq ifr;
+ VIR_AUTOCLOSE(fd);
if ((fd = virNetDevSetupControl(brname, &ifr)) < 0)
return -1;
@@ -647,19 +618,16 @@ int virNetDevBridgeAddPort(const char *brname,
if (!(ifr.ifr_ifindex = if_nametoindex(ifname))) {
virReportSystemError(ENODEV,
_("Unable to get interface index for %s"),
ifname);
- goto cleanup;
+ return -1;
}
if (ioctl(fd, SIOCBRADDIF, &ifr) < 0) {
virReportSystemError(errno,
_("Unable to add bridge %s port %s"), brname,
ifname);
- goto cleanup;
+ return -1;
}
- ret = 0;
- cleanup:
- VIR_FORCE_CLOSE(fd);
- return ret;
+ return 0;
}
#elif defined(HAVE_BSD_BRIDGE_MGMT)
int virNetDevBridgeAddPort(const char *brname,
@@ -706,9 +674,8 @@ int virNetDevBridgeAddPort(const char *brname,
int virNetDevBridgeRemovePort(const char *brname,
const char *ifname)
{
- int fd = -1;
- int ret = -1;
struct ifreq ifr;
+ VIR_AUTOCLOSE(fd);
if ((fd = virNetDevSetupControl(brname, &ifr)) < 0)
return -1;
@@ -717,19 +684,16 @@ int virNetDevBridgeRemovePort(const char *brname,
virReportSystemError(ENODEV,
_("Unable to get interface index for %s"),
ifname);
- goto cleanup;
+ return -1;
}
if (ioctl(fd, SIOCBRDELIF, &ifr) < 0) {
virReportSystemError(errno,
_("Unable to remove bridge %s port %s"), brname,
ifname);
- goto cleanup;
+ return -1;
}
- ret = 0;
- cleanup:
- VIR_FORCE_CLOSE(fd);
- return ret;
+ return 0;
}
#elif defined(HAVE_BSD_BRIDGE_MGMT)
int virNetDevBridgeRemovePort(const char *brname,
@@ -778,19 +742,14 @@ int virNetDevBridgeRemovePort(const char *brname,
int virNetDevBridgeSetSTPDelay(const char *brname,
int delay)
{
- int fd = -1;
- int ret = -1;
struct ifreq ifr;
+ VIR_AUTOCLOSE(fd);
if ((fd = virNetDevSetupControl(brname, &ifr)) < 0)
- goto cleanup;
+ return -1;
- ret = virNetDevBridgeSet(brname, "forward_delay", MS_TO_JIFFIES(delay),
+ return virNetDevBridgeSet(brname, "forward_delay", MS_TO_JIFFIES(delay),
fd, &ifr);
-
- cleanup:
- VIR_FORCE_CLOSE(fd);
- return ret;
}
@@ -831,19 +790,14 @@ int virNetDevBridgeGetSTPDelay(const char *brname,
int virNetDevBridgeSetSTP(const char *brname,
bool enable)
{
- int fd = -1;
- int ret = -1;
struct ifreq ifr;
+ VIR_AUTOCLOSE(fd);
if ((fd = virNetDevSetupControl(brname, &ifr)) < 0)
- goto cleanup;
+ return -1;
- ret = virNetDevBridgeSet(brname, "stp_state", enable ? 1 : 0,
+ return virNetDevBridgeSet(brname, "stp_state", enable ? 1 : 0,
fd, &ifr);
-
- cleanup:
- VIR_FORCE_CLOSE(fd);
- return ret;
}
--
2.17.1