Fix leaks in udev device add/remove v3

* 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
This commit is contained in:
David Allan 2010-05-28 22:22:05 -04:00
parent 77da2487ea
commit e7f3bad46e

View 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;
}