* 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 | 12 ++++++++++--
1 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index 6e3ecd7..73217c5 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -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;
}
--
1.7.0.1