]> xenbits.xensource.com Git - libvirt.git/commitdiff
Fix leaks in udev device add/remove v3
authorDavid Allan <dallan@redhat.com>
Sat, 29 May 2010 02:22:05 +0000 (22:22 -0400)
committerDavid Allan <dallan@redhat.com>
Tue, 8 Jun 2010 18:46:29 +0000 (14:46 -0400)
* This patch is a modification of a patch submitted by Nigel Jones.
  It fixes several memory leaks on device addition/removal:

1. Free the virNodeDeviceDefPtr in udevAddOneDevice if the return
   value is non-zero

2. Always release the node device reference after the device has been
   processed.

* Refactored for better readability per the suggestion of clalance

src/node_device/node_device_udev.c

index 6e3ecd7578ccc368d709e3acc294fd2cc1fe0315..73217c5e0c0dcac341cfeccdf012c9c2b0ba19c1 100644 (file)
@@ -1309,13 +1309,14 @@ static int udevAddOneDevice(struct udev_device *device)
         goto out;
     }
 
+    /* If this is a device change, the old definition will be freed
+     * and the current definition will take its place. */
     nodeDeviceLock(driverState);
     dev = virNodeDeviceAssignDef(&driverState->devs, def);
     nodeDeviceUnlock(driverState);
 
     if (dev == NULL) {
         VIR_ERROR(_("Failed to create device for '%s'"), def->name);
-        virNodeDeviceDefFree(def);
         goto out;
     }
 
@@ -1324,6 +1325,10 @@ static int udevAddOneDevice(struct udev_device *device)
     ret = 0;
 
 out:
+    if (ret != 0) {
+        virNodeDeviceDefFree(def);
+    }
+
     return ret;
 }
 
@@ -1338,15 +1343,17 @@ static int udevProcessDeviceListEntry(struct udev *udev,
     name = udev_list_entry_get_name(list_entry);
 
     device = udev_device_new_from_syspath(udev, name);
+
     if (device != NULL) {
         if (udevAddOneDevice(device) != 0) {
             VIR_INFO("Failed to create node device for udev device '%s'",
                      name);
         }
-        udev_device_unref(device);
         ret = 0;
     }
 
+    udev_device_unref(device);
+
     return ret;
 }
 
@@ -1454,6 +1461,7 @@ static void udevEventHandleCallback(int watch ATTRIBUTE_UNUSED,
     }
 
 out:
+    udev_device_unref(device);
     return;
 }