[Libvirt-cim] [PATCH 09/10] DevicePool: Use the virConnectListAll interfaces

John Ferlan jferlan at redhat.com
Thu Mar 14 22:56:01 UTC 2013


Rather than the somewhat unreliable get a count and get a list of active
names, use the newer virConnectListAll* interfaces in order to retrieve both
a count and list in one call.
---
 src/Virt_DevicePool.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 119 insertions(+), 2 deletions(-)

diff --git a/src/Virt_DevicePool.c b/src/Virt_DevicePool.c
index bf3dd3b..bffa0cf 100644
--- a/src/Virt_DevicePool.c
+++ b/src/Virt_DevicePool.c
@@ -146,13 +146,24 @@ static int get_diskpool_config(virConnectPtr conn,
                                struct tmp_disk_pool **_pools,
                                int *_count)
 {
+#if LIBVIR_VERSION_NUMBER >= 100002
+        int i, realcount = 0, count = 0;
+        virStoragePoolPtr *nameList = NULL;
+        int flags = VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE;
+#else
         int count = 0, realcount = 0;
         int i;
         char ** names = NULL;
+#endif
         struct tmp_disk_pool *pools = NULL;
         int ret = 0;
         bool bret;
 
+#if LIBVIR_VERSION_NUMBER >= 100002
+        realcount = virConnectListAllStoragePools(conn,
+                                                  &nameList,
+                                                  flags);
+#else
         count = virConnectNumOfStoragePools(conn);
         if (count < 0) {
                 ret = count;
@@ -169,6 +180,7 @@ static int get_diskpool_config(virConnectPtr conn,
         }
 
         realcount = virConnectListStoragePools(conn, names, count);
+#endif
         if (realcount < 0) {
                 CU_DEBUG("Failed to get storage pools, return %d.", realcount);
                 ret = realcount;
@@ -187,7 +199,13 @@ static int get_diskpool_config(virConnectPtr conn,
         }
 
         for (i = 0; i < realcount; i++) {
-                pools[i].tag = strdup(names[i]);
+#if LIBVIR_VERSION_NUMBER >= 100002
+                pools[i].tag = strdup(getVirStoragePoolName(nameList[i]));
+#else
+                /* Just take names[i], since we're free()'ing later */
+                pools[i].tag = names[i];
+                names[i] = NULL;
+#endif
                 if (pools[i].tag == NULL) {
                         CU_DEBUG("Failed in strdup for name '%s'.", names[i]);
                         ret = -3;
@@ -213,10 +231,18 @@ static int get_diskpool_config(virConnectPtr conn,
         free_diskpool(pools, realcount);
 
  free_names:
+#if LIBVIR_VERSION_NUMBER >= 100002
+        if (nameList != NULL) {
+                for (i = 0; i < realcount; i++)
+                        virStoragePoolFree(nameList[i]);
+                free(nameList);
+        }
+#else
         for (i = 0; i < count; i++) {
                 free(names[i]);
         }
         free(names);
+#endif
 
  out:
         return ret;
@@ -529,6 +555,40 @@ static char *diskpool_member_of(const CMPIBroker *broker,
 static virNetworkPtr bridge_to_network(virConnectPtr conn,
                                        const char *bridge)
 {
+#if LIBVIR_VERSION_NUMBER >= 100002
+        int num;
+        virNetworkPtr **nameList = NULL;
+        virNetworkPtr network = NULL;
+        flags = VIR_CONNECT_LIST_NETWORKS_ACTIVE;
+
+        num = virConnectListAllNetworks(conn,
+                                        &nameList,
+                                        flags);
+        if (num < 0) {
+                CU_DEBUG("Failed to get network pools.");
+                return NULL;
+        }
+
+        for (i = 0; i < num; i++) {
+                char *_netname;
+                char *_bridge;
+
+                _netname = virNetworkGetName(nameList[i]);
+                _bridge = virNetworkGetBridgeName(network);
+                CU_DEBUG("Network `%s' has bridge `%s'", _netname, _bridge);
+                if (STREQ(bridge, _bridge)) {
+                        network = nameList[i];
+                        nameList[i] = NULL;
+                        i = num;        /* Loop breaker */
+                }
+                free(_bridge);
+        }
+
+        for (i = 0; i < num; i++) {
+                virNetworkFree(nameList[i]);
+        }
+        free(nameList);
+#else
         char **networks = NULL;
         virNetworkPtr network = NULL;
         int num;
@@ -566,7 +626,7 @@ static virNetworkPtr bridge_to_network(virConnectPtr conn,
         for (i = 0; i < num; i++)
                 free(networks[i]);
         free(networks);
-
+#endif
         return network;
 }
 
@@ -748,6 +808,29 @@ static bool mempool_set_total(CMPIInstance *inst, virConnectPtr conn)
 static bool mempool_set_consumed(CMPIInstance *inst, virConnectPtr conn)
 {
         uint64_t memory = 0;
+#if LIBVIR_VERSION_NUMBER >= 9013
+        virDomainPtr *nameList = NULL;
+        int n_names, i;
+        int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE;
+
+        n_names = virConnectListAllDomains(conn,
+                                           &nameList,
+                                           flags);
+        if (n_names < 0) {
+                CU_DEBUG("Failed to get a list of all domains");
+                goto out;
+        }
+
+        for (i = 0; i < n_names; i++) {
+                virDomainInfo dom_info;
+                if (virDomainGetInfo(nameList[i], &dom_info) == 0)
+                        memory += dom_info.memory;
+                virDomainFree(nameList[i]);
+        }
+        free(nameList);
+
+ out:
+#else
         int *domain_ids = NULL;
         int count, i = 0;
 
@@ -781,6 +864,7 @@ static bool mempool_set_consumed(CMPIInstance *inst, virConnectPtr conn)
 
  out:
         free(domain_ids);
+#endif /*  LIBVIR_VERSION_NUMBER >= 0913 */
 
         CMSetProperty(inst, "Reserved",
                       (CMPIValue *)&memory, CMPI_uint64);
@@ -1034,6 +1118,10 @@ static CMPIStatus netpool_instance(virConnectPtr conn,
         char **netnames = NULL;
         int i;
         int nets = 0;
+#if LIBVIR_VERSION_NUMBER >= 100002
+        virNetworkPtr **nameList = NULL;
+        flags = VIR_CONNECT_LIST_NETWORKS_ACTIVE;
+#endif
 
         if (id != NULL) {
                 return _netpool_for_network(list,
@@ -1044,7 +1132,16 @@ static CMPIStatus netpool_instance(virConnectPtr conn,
                                             broker);
         }
 
+#if LIBVIR_VERSION_NUMBER >= 100002
+        nets = virConnectListAllNetworks(conn,
+                                         &nameList,
+                                         flags);
+        /* Avoids the need to realloc since we have a true number */
+        if (nets >= 0)
+            nets++;
+#else
         nets = virConnectNumOfNetworks(conn);
+#endif
         if (nets < 0) {
                 virt_set_status(broker, &s,
                                 CMPI_RC_ERR_FAILED,
@@ -1062,6 +1159,18 @@ static CMPIStatus netpool_instance(virConnectPtr conn,
                 goto out;
         }
 
+#if LIBVIR_VERSION_NUMBER >= 100002
+        for (i = 0; i < nets - 1; i++) {
+                netnames[i] = strdup(virNetworkGetName(nameList[i]));
+                if (netnames[i] == NULL) {
+                    cu_statusf(broker, &s,
+                               CMPI_RC_ERR_FAILED,
+                               "Failed to strdup memory for %i net names",
+                               nets);
+                    goto out;
+                }
+        }
+#else
         nets = virConnectListNetworks(conn, netnames, nets);
 
         nets++;
@@ -1072,6 +1181,7 @@ static CMPIStatus netpool_instance(virConnectPtr conn,
                            "Failed to allocate memory for %i net names", nets);
                 goto out;
         }
+#endif
 
         netnames[nets - 1] = strdup("0");
 
@@ -1085,6 +1195,13 @@ static CMPIStatus netpool_instance(virConnectPtr conn,
         }
 
  out:
+#if LIBVIR_VERSION_NUMBER >= 100002
+        if (nameList != NULL) {
+                for (i = 0; i < nets - 1; i++)
+                        virNetworkFree(nameList[i]);
+                free(nameList);
+        }
+#endif
         if (netnames != NULL) {
                 for (i = 0; i < nets; i++)
                         free(netnames[i]);
-- 
1.8.1.4




More information about the Libvirt-cim mailing list