]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
virtportallocator: new function "virPortAllocatorSetUsed"
authorGiuseppe Scrivano <gscrivan@redhat.com>
Tue, 24 Jun 2014 11:34:17 +0000 (13:34 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 24 Jun 2014 12:17:55 +0000 (14:17 +0200)
virPortAllocatorSetUsed permits to set a port as already used and
prevent the port allocator to use it without any attempt to bind it.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/util/virportallocator.c
src/util/virportallocator.h

index fd0191307238d491de61af688a6709e7d65eee73..981dc879f0bfc68c82662da558008e01f40c8b0f 100644 (file)
@@ -1770,6 +1770,7 @@ virPidFileWritePath;
 virPortAllocatorAcquire;
 virPortAllocatorNew;
 virPortAllocatorRelease;
+virPortAllocatorSetUsed;
 
 
 # util/virprocess.h
index b68133a1b427a10897bd18224650ee16abe5ae97..ff5691a2b7db0610312cb1d659384dd5387a22a6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * virportallocator.c: Allocate & track TCP port allocations
  *
- * Copyright (C) 2013 Red Hat, Inc.
+ * Copyright (C) 2013-2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -250,3 +250,44 @@ int virPortAllocatorRelease(virPortAllocatorPtr pa,
     virObjectUnlock(pa);
     return ret;
 }
+
+int virPortAllocatorSetUsed(virPortAllocatorPtr pa,
+                            unsigned short port,
+                            bool value)
+{
+    int ret = -1;
+
+    virObjectLock(pa);
+
+    if (port < pa->start ||
+        port > pa->end) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    if (value) {
+        bool used = false;
+        if (virBitmapGetBit(pa->bitmap, port - pa->start, &used) < 0)
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to query port %d"), port);
+
+        if (used || virBitmapSetBit(pa->bitmap, port - pa->start) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to reserve port %d"), port);
+            goto cleanup;
+        }
+    } else {
+        if (virBitmapClearBit(pa->bitmap,
+                              port - pa->start) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to release port %d"),
+                           port);
+            goto cleanup;
+        }
+    }
+
+    ret = 0;
+ cleanup:
+    virObjectUnlock(pa);
+    return ret;
+}
index c8aa6deffd265341c2d001a89f56dd284709826f..e5ee56d8d9f19f1865420d51aaed9e051de01284 100644 (file)
@@ -38,4 +38,8 @@ int virPortAllocatorAcquire(virPortAllocatorPtr pa,
 int virPortAllocatorRelease(virPortAllocatorPtr pa,
                             unsigned short port);
 
+int virPortAllocatorSetUsed(virPortAllocatorPtr pa,
+                            unsigned short port,
+                            bool value);
+
 #endif /* __VIR_PORT_ALLOCATOR_H__ */