[libvirt PATCH 09/12] nwfilter_ipaddrmap: Use automatic mutex management

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx>
---
 src/conf/nwfilter_ipaddrmap.c | 80 ++++++++++++++---------------------
 1 file changed, 31 insertions(+), 49 deletions(-)

diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c
index e2f123b9d9..4090cc2769 100644
--- a/src/conf/nwfilter_ipaddrmap.c
+++ b/src/conf/nwfilter_ipaddrmap.c
@@ -49,37 +49,28 @@ static GHashTable *ipAddressMap;
 int
 virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr)
 {
-    int ret = -1;
-    char *addrCopy;
+    g_autofree char *addrCopy = g_strdup(addr);
+    VIR_LOCK_GUARD lock = virLockGuardLock(&ipAddressMapLock);
     virNWFilterVarValue *val;
 
-    addrCopy = g_strdup(addr);
-
-    virMutexLock(&ipAddressMapLock);
-
-    val = virHashLookup(ipAddressMap, ifname);
-    if (!val) {
-        val = virNWFilterVarValueCreateSimple(addrCopy);
-        if (!val)
-            goto cleanup;
-        addrCopy = NULL;
-        ret = virHashUpdateEntry(ipAddressMap, ifname, val);
-        if (ret < 0)
-            virNWFilterVarValueFree(val);
-        goto cleanup;
-    } else {
+    if ((val = virHashLookup(ipAddressMap, ifname)) != NULL) {
         if (virNWFilterVarValueAddValue(val, addrCopy) < 0)
-            goto cleanup;
+            return -1;
+
         addrCopy = NULL;
+        return 0;
     }
 
-    ret = 0;
+    if ((val = virNWFilterVarValueCreateSimple(addrCopy)) == NULL)
+        return -1;
 
- cleanup:
-    virMutexUnlock(&ipAddressMapLock);
-    VIR_FREE(addrCopy);
+    addrCopy = NULL;
+    if (virHashUpdateEntry(ipAddressMap, ifname, val) < 0) {
+        virNWFilterVarValueFree(val);
+        return -1;
+    }
 
-    return ret;
+    return 0;
 }
 
 /* Delete all or a specific IP address from an interface. After this
@@ -99,31 +90,28 @@ virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr)
 int
 virNWFilterIPAddrMapDelIPAddr(const char *ifname, const char *ipaddr)
 {
-    int ret = -1;
+    VIR_LOCK_GUARD lock = virLockGuardLock(&ipAddressMapLock);
     virNWFilterVarValue *val = NULL;
 
-    virMutexLock(&ipAddressMapLock);
-
-    if (ipaddr != NULL) {
-        val = virHashLookup(ipAddressMap, ifname);
-        if (val) {
-            if (virNWFilterVarValueGetCardinality(val) == 1 &&
-                STREQ(ipaddr,
-                      virNWFilterVarValueGetNthValue(val, 0)))
-                goto remove_entry;
-            virNWFilterVarValueDelValue(val, ipaddr);
-            ret = virNWFilterVarValueGetCardinality(val);
-        }
-    } else {
- remove_entry:
+    if (!ipaddr) {
         /* remove whole entry */
         virHashRemoveEntry(ipAddressMap, ifname);
-        ret = 0;
+        return 0;
+    }
+
+    if (!(val = virHashLookup(ipAddressMap, ifname))) {
+        return -1;
     }
 
-    virMutexUnlock(&ipAddressMapLock);
+    if (virNWFilterVarValueGetCardinality(val) == 1 &&
+        STREQ(ipaddr, virNWFilterVarValueGetNthValue(val, 0))) {
+        /* remove whole entry */
+        virHashRemoveEntry(ipAddressMap, ifname);
+        return 0;
+    }
 
-    return ret;
+    virNWFilterVarValueDelValue(val, ipaddr);
+    return virNWFilterVarValueGetCardinality(val);
 }
 
 /* Get the list of IP addresses known to be in use by an interface
@@ -135,15 +123,9 @@ virNWFilterIPAddrMapDelIPAddr(const char *ifname, const char *ipaddr)
 virNWFilterVarValue *
 virNWFilterIPAddrMapGetIPAddr(const char *ifname)
 {
-    virNWFilterVarValue *res;
-
-    virMutexLock(&ipAddressMapLock);
-
-    res = virHashLookup(ipAddressMap, ifname);
-
-    virMutexUnlock(&ipAddressMapLock);
+    VIR_LOCK_GUARD lock = virLockGuardLock(&ipAddressMapLock);
 
-    return res;
+    return virHashLookup(ipAddressMap, ifname);
 }
 
 int
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux