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,9 +212,8 @@ 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++) {
for (i = range->start; i <= range->end && !*port; i++) {
bool used = false, v6used = false; bool used = false, v6used = false;
if (virBitmapIsBitSet(pa->bitmap, i)) if (virBitmapIsBitSet(pa->bitmap, i))
@ -223,34 +221,30 @@ virPortAllocatorAcquire(const virPortAllocatorRange *range,
if (virPortAllocatorBindToPort(&v6used, i, AF_INET6) < 0 || if (virPortAllocatorBindToPort(&v6used, i, AF_INET6) < 0 ||
virPortAllocatorBindToPort(&used, i, AF_INET) < 0) virPortAllocatorBindToPort(&used, i, AF_INET) < 0)
goto cleanup; return -1;
if (!used && !v6used) { if (!used && !v6used) {
/* Add port to bitmap of reserved ports */ /* Add port to bitmap of reserved ports */
if (virBitmapSetBit(pa->bitmap, i) < 0) { if (virBitmapSetBit(pa->bitmap, i) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to reserve port %zu"), i); _("Failed to reserve port %zu"), i);
goto cleanup; return -1;
} }
*port = i; *port = i;
ret = 0; return 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);
goto cleanup; return -1;
}
} }
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);
goto cleanup; return -1;
}
} }
ret = 0; return 0;
cleanup:
virObjectUnlock(pa);
return ret;
} }