[Libvirt-cim] [PATCH 06/10] libxkutil: Use virConnectListAllDomains() to fetch domains

John Ferlan jferlan at redhat.com
Thu Mar 14 22:55:58 UTC 2013


This is an optimization over using the multistep approach to get a count,
get some memory, and get the list of domains (active and defined). Followed
other examples to ensure only building the code if the libvirt version is
correct. The API was added in 0.9.13.
---
 libxkutil/cs_util_instance.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/libxkutil/cs_util_instance.c b/libxkutil/cs_util_instance.c
index a383147..e34c05d 100644
--- a/libxkutil/cs_util_instance.c
+++ b/libxkutil/cs_util_instance.c
@@ -34,6 +34,28 @@
 #include <libcmpiutil/libcmpiutil.h>
 
 int get_domain_list(virConnectPtr conn, virDomainPtr **_list)
+#if LIBVIR_VERSION_NUMBER >= 9013
+{
+        virDomainPtr *nameList = NULL;
+        int n_names;
+        int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
+                    VIR_CONNECT_LIST_DOMAINS_INACTIVE;
+
+        n_names = virConnectListAllDomains(conn,
+                                           &nameList,
+                                           flags);
+        if (n_names > 0) {
+                *_list = nameList;
+        } else if (n_names == 0) {
+                /* Since there are no elements, no domain ptrs to free
+                 * but still must free the nameList returned
+                 */
+                free(nameList);
+        }
+
+        return n_names;
+}
+#else
 {
         char **names = NULL;
         int n_names;
@@ -113,6 +135,7 @@ int get_domain_list(virConnectPtr conn, virDomainPtr **_list)
 
         return idx;
 }
+#endif /* LIBVIR_VERSION_NUMBER >= 0913 */
 
 void set_instance_class_name(CMPIInstance *instance, char *name)
 {
-- 
1.8.1.4




More information about the Libvirt-cim mailing list