]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virportallocator: Use automatic mutex management
authorTim Wiederhake <twiederh@redhat.com>
Fri, 25 Mar 2022 09:37:03 +0000 (10:37 +0100)
committerTim Wiederhake <twiederh@redhat.com>
Tue, 5 Apr 2022 13:59:08 +0000 (15:59 +0200)
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virportallocator.c

index 5e634708485288a0b217b7feaab4e24b650b72da..44edd8f99a54d5c4bd15057d7ec112c8bf352402 100644 (file)
@@ -204,7 +204,6 @@ int
 virPortAllocatorAcquire(const virPortAllocatorRange *range,
                         unsigned short *port)
 {
-    int ret = -1;
     size_t i;
     virPortAllocator *pa = virPortAllocatorGet();
 
@@ -213,44 +212,39 @@ virPortAllocatorAcquire(const virPortAllocatorRange *range,
     if (!pa)
         return -1;
 
-    virObjectLock(pa);
-
-    for (i = range->start; i <= range->end && !*port; i++) {
-        bool used = false, v6used = false;
-
-        if (virBitmapIsBitSet(pa->bitmap, i))
-            continue;
-
-        if (virPortAllocatorBindToPort(&v6used, i, AF_INET6) < 0 ||
-            virPortAllocatorBindToPort(&used, i, AF_INET) < 0)
-            goto cleanup;
-
-        if (!used && !v6used) {
-            /* Add port to bitmap of reserved ports */
-            if (virBitmapSetBit(pa->bitmap, i) < 0) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("Failed to reserve port %zu"), i);
-                goto cleanup;
+    VIR_WITH_OBJECT_LOCK_GUARD(pa) {
+        for (i = range->start; i <= range->end; i++) {
+            bool used = false, v6used = false;
+
+            if (virBitmapIsBitSet(pa->bitmap, i))
+                continue;
+
+            if (virPortAllocatorBindToPort(&v6used, i, AF_INET6) < 0 ||
+                virPortAllocatorBindToPort(&used, i, AF_INET) < 0)
+                return -1;
+
+            if (!used && !v6used) {
+                /* Add port to bitmap of reserved ports */
+                if (virBitmapSetBit(pa->bitmap, i) < 0) {
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("Failed to reserve port %zu"), i);
+                    return -1;
+                }
+                *port = i;
+                return 0;
             }
-            *port = i;
-            ret = 0;
         }
     }
 
-    if (*port == 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Unable to find an unused port in range '%s' (%d-%d)"),
-                       range->name, range->start, range->end);
-    }
- cleanup:
-    virObjectUnlock(pa);
-    return ret;
+    virReportError(VIR_ERR_INTERNAL_ERROR,
+                   _("Unable to find an unused port in range '%s' (%d-%d)"),
+                   range->name, range->start, range->end);
+    return -1;
 }
 
 int
 virPortAllocatorRelease(unsigned short port)
 {
-    int ret = -1;
     virPortAllocator *pa = virPortAllocatorGet();
 
     if (!pa)
@@ -259,25 +253,21 @@ virPortAllocatorRelease(unsigned short port)
     if (!port)
         return 0;
 
-    virObjectLock(pa);
-
-    if (virBitmapClearBit(pa->bitmap, port) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Failed to release port %d"),
-                       port);
-        goto cleanup;
+    VIR_WITH_OBJECT_LOCK_GUARD(pa) {
+        if (virBitmapClearBit(pa->bitmap, port) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to release port %d"),
+                           port);
+            return -1;
+        }
     }
 
-    ret = 0;
- cleanup:
-    virObjectUnlock(pa);
-    return ret;
+    return 0;
 }
 
 int
 virPortAllocatorSetUsed(unsigned short port)
 {
-    int ret = -1;
     virPortAllocator *pa = virPortAllocatorGet();
 
     if (!pa)
@@ -286,17 +276,14 @@ virPortAllocatorSetUsed(unsigned short port)
     if (!port)
         return 0;
 
-    virObjectLock(pa);
-
-    if (virBitmapIsBitSet(pa->bitmap, port) ||
-        virBitmapSetBit(pa->bitmap, port) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Failed to reserve port %d"), port);
-        goto cleanup;
+    VIR_WITH_OBJECT_LOCK_GUARD(pa) {
+        if (virBitmapIsBitSet(pa->bitmap, port) ||
+            virBitmapSetBit(pa->bitmap, port) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to reserve port %d"), port);
+            return -1;
+        }
     }
 
-    ret = 0;
- cleanup:
-    virObjectUnlock(pa);
-    return ret;
+    return 0;
 }