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

John Ferlan jferlan at redhat.com
Fri Mar 22 20:47:11 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 | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/libxkutil/cs_util_instance.c b/libxkutil/cs_util_instance.c
index a383147..e95052a 100644
--- a/libxkutil/cs_util_instance.c
+++ b/libxkutil/cs_util_instance.c
@@ -33,6 +33,29 @@
 #include "cs_util.h"
 #include <libcmpiutil/libcmpiutil.h>
 
+#if LIBVIR_VERSION_NUMBER >= 9013
+int get_domain_list(virConnectPtr conn, virDomainPtr **_list)
+{
+        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
 int get_domain_list(virConnectPtr conn, virDomainPtr **_list)
 {
         char **names = NULL;
@@ -113,6 +136,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