virportallocator: Use automatic mutex management

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Tim Wiederhake 2022-03-25 10:37:03 +01:00
parent 5b48de594d
commit e53c02ea20

View File

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