Author: Adam Majer <amajer@suse.de>
Date: Backport memory leak fixes


commit f6b7eeaf097b6441ed7928c7130582a83bba5f7d
Author: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
Date:   Fri Sep 6 14:09:55 2013 +0200

    libxkutil: Plug memory leaks in device parsing
    
    Fixed a number of memory leaks detected while running xml_parse_test
    under valgrind.
    
    Signed-off-by: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
    Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>


commit 2be18f4acf32e33674138df1ae0f47d358b2fdac
Author: Sharad Mishra <snmishra@us.ibm.com>
Date:   Wed May 18 09:35:05 2011 -0700

    (#3) Fix connection leaks.
    
    libvirt-cim had few connection leaks which were causing it to crash.
    
    Signed-off-by: Sharad Mishra <snmishra@us.ibm.com>
    
     #2 - cimtest showed another leak, which was fixed in this version.
     #3 - Review comments to move and rename dom2 - virDomainPtr to  _dom.
          Move virConnectClose during migration to migrate_do.



Index: libvirt-cim-0.5.8/src/Virt_AllocationCapabilities.c
===================================================================
--- libvirt-cim-0.5.8.orig/src/Virt_AllocationCapabilities.c
+++ libvirt-cim-0.5.8/src/Virt_AllocationCapabilities.c
@@ -79,7 +79,6 @@ CMPIStatus enum_alloc_cap_instances(cons
                                     const char *id,
                                     struct inst_list *list)
 {
-        virConnectPtr conn = NULL;
         CMPIInstance *alloc_cap_inst;
         struct inst_list device_pool_list;
         CMPIStatus s = {CMPI_RC_OK, NULL};
@@ -91,15 +90,6 @@ CMPIStatus enum_alloc_cap_instances(cons
         if (!provider_is_responsible(broker, ref, &s))
                 goto out;
 
-        conn = connect_by_classname(broker, CLASSNAME(ref), &s);
-        if (conn == NULL) {
-                if (id) 
-                        cu_statusf(broker, &s,
-                                   CMPI_RC_ERR_NOT_FOUND,
-                                   "Instance not found.");
-                goto out;
-        }
-
         s = enum_pools(broker, ref, CIM_RES_TYPE_ALL, &device_pool_list);
         if (s.rc != CMPI_RC_OK) {
                 cu_statusf(broker, &s,
@@ -141,7 +131,6 @@ CMPIStatus enum_alloc_cap_instances(cons
         }
         
  out:
-        virConnectClose(conn);
         inst_list_free(&device_pool_list);
 
         return s;
Index: libvirt-cim-0.5.8/src/Virt_ComputerSystem.c
===================================================================
--- libvirt-cim-0.5.8.orig/src/Virt_ComputerSystem.c
+++ libvirt-cim-0.5.8/src/Virt_ComputerSystem.c
@@ -907,6 +907,7 @@ static CMPIStatus domain_reset(virDomain
 {
         int ret;
         virConnectPtr conn = NULL;
+        virDomainPtr _dom = NULL;
         char *xml = NULL;
         CMPIStatus s = {CMPI_RC_OK, NULL};
 
@@ -938,12 +939,12 @@ static CMPIStatus domain_reset(virDomain
                 goto out;
         }
 
-        dom = virDomainLookupByName(conn,
+        _dom = virDomainLookupByName(conn,
                                     virDomainGetName(dom));
 
-        if (dom == NULL) {
-            dom = virDomainDefineXML(conn, xml);
-            if (dom == NULL) {
+        if (_dom == NULL) {
+            _dom = virDomainDefineXML(conn, xml);
+            if (_dom == NULL) {
                     CU_DEBUG("Failed to define domain from XML");
                     virt_set_status(_BROKER, &s,
                                     CMPI_RC_ERR_FAILED,
@@ -953,10 +954,10 @@ static CMPIStatus domain_reset(virDomain
             }
         }
 
-        if (!domain_online(dom))
+        if (!domain_online(_dom))
             CU_DEBUG("Guest is now offline");
 
-        ret = virDomainCreate(dom);
+        ret = virDomainCreate(_dom);
         if (ret != 0)
                 virt_set_status(_BROKER, &s,
                                 CMPI_RC_ERR_FAILED,
@@ -965,6 +966,7 @@ static CMPIStatus domain_reset(virDomain
 
  out:
         free(xml);
+        virDomainFree(_dom);
 
         return s;
 }
Index: libvirt-cim-0.5.8/src/Virt_ComputerSystemIndication.c
===================================================================
--- libvirt-cim-0.5.8.orig/src/Virt_ComputerSystemIndication.c
+++ libvirt-cim-0.5.8/src/Virt_ComputerSystemIndication.c
@@ -412,7 +412,6 @@ static bool create_deleted_guest_inst(ch
 }
 
 static bool async_ind(CMPIContext *context,
-                      virConnectPtr conn,
                       int ind_type,
                       struct dom_xml prev_dom,
                       char *prefix,
@@ -547,7 +546,7 @@ static CMPI_THREAD_RETURN lifecycle_thre
                 for (i = 0; i < cur_count; i++) {
                         res = dom_in_list(cur_xml[i].uuid, prev_count, prev_xml);
                         if (!res)
-                                async_ind(context, conn, CS_CREATED,
+                                async_ind(context, CS_CREATED,
                                           cur_xml[i], prefix, args);
 
                 }
@@ -555,14 +554,14 @@ static CMPI_THREAD_RETURN lifecycle_thre
                 for (i = 0; i < prev_count; i++) {
                         res = dom_in_list(prev_xml[i].uuid, cur_count, cur_xml);
                         if (!res)
-                                async_ind(context, conn, CS_DELETED, 
+                                async_ind(context, CS_DELETED, 
                                           prev_xml[i], prefix, args);
                 }
 
                 for (i = 0; i < prev_count; i++) {
                         res = dom_changed(prev_xml[i], cur_xml, cur_count);
                         if (res) {
-                                async_ind(context, conn, CS_MODIFIED, 
+                                async_ind(context, CS_MODIFIED, 
                                           prev_xml[i], prefix, args);
 
                         }
Index: libvirt-cim-0.5.8/src/Virt_DevicePool.c
===================================================================
--- libvirt-cim-0.5.8.orig/src/Virt_DevicePool.c
+++ libvirt-cim-0.5.8/src/Virt_DevicePool.c
@@ -427,8 +427,8 @@ static char *diskpool_member_of(const CM
 
         free(host);
         free(dev);
-        virConnectClose(conn);
         virDomainFree(dom);
+        virConnectClose(conn);
 
         return pool;
 }
Index: libvirt-cim-0.5.8/libxkutil/device_parsing.c
===================================================================
--- libvirt-cim-0.5.8.orig/libxkutil/device_parsing.c
+++ libvirt-cim-0.5.8/libxkutil/device_parsing.c
@@ -64,6 +64,7 @@ static void cleanup_net_device(struct ne
         free(dev->mac);
         free(dev->source);
         free(dev->model);
+//        cleanup_vsi_device(&dev->vsi);
 }
 
 static void cleanup_emu_device(struct emu_device *dev)
@@ -577,7 +578,10 @@ static int do_parse(xmlNodeSet *nsv, int
         }
 
   out:
-        *l = list;
+        if (list) {
+                free(*l);
+                *l = list;
+        }
         return lstidx;
 }
 
@@ -883,7 +887,7 @@ static int parse_features(struct domain
 
 static void set_action(int *val, xmlNode *child)
 {
-        const char *action = (char *)xmlNodeGetContent(child);
+        char *action = (char *)xmlNodeGetContent(child);
 
         if (action == NULL)
                 *val = CIM_VSSD_RECOVERY_NONE;
@@ -895,6 +899,8 @@ static void set_action(int *val, xmlNode
                 *val = CIM_VSSD_RECOVERY_RESTART;
         else
                 *val = CIM_VSSD_RECOVERY_NONE;
+
+       xmlFree(action);
 }
 
 static int parse_domain(xmlNodeSet *nsv, struct domain *dominfo)
@@ -1027,9 +1033,11 @@ void cleanup_dominfo(struct domain **dom
                 return;
 
         free(dom->name);
+        free(dom->typestr);
         free(dom->uuid);
         free(dom->bootloader);
         free(dom->bootloader_args);
+        free(dom->clock);
 
         if (dom->type == DOMAIN_XENPV) {
                 free(dom->os_info.pv.type);
@@ -1054,6 +1062,7 @@ void cleanup_dominfo(struct domain **dom
                 CU_DEBUG("Unknown domain type %i", dom->type);
         }
 
+        cleanup_virt_devices(&dom->dev_emu, 1);
         cleanup_virt_devices(&dom->dev_mem, dom->dev_mem_ct);
         cleanup_virt_devices(&dom->dev_net, dom->dev_net_ct);
         cleanup_virt_devices(&dom->dev_disk, dom->dev_disk_ct);
