Linux: Fix #70 race condition in sysfs_get_device_list()
authorAlan Ott <alan@signal11.us>
Thu, 21 Jul 2011 14:37:48 +0000 (16:37 +0200)
committerPeter Stuge <peter@stuge.se>
Sun, 24 Jul 2011 20:28:31 +0000 (22:28 +0200)
commit5f30c81f66e8dd61f8eae16de548697708f9bd18
tree0bbd0a2a7068c290426af8b60875d38337548b2b
parent40327cd134718475f6cec8935b856d4fdff2099c
Linux: Fix #70 race condition in sysfs_get_device_list()

Change the way libusb chooses between using sysfs and usbfs for information
about the attached devies.  Using the old method, a race condition could
occur if a device was unplugged just before (or during) the call to
libusb_get_device_list(), corrupting the internal sysfs_can_relate_devices
and sysfs_has_descriptors variables and preventing libusb_get_device_list()
from working in future calls.

The old method was based on the assumption that if certain sysfs files
(eg: busnum) could not be opened, that indicated an inadequacy of sysfs
(ie: the running kernel's sysfs version did not contain those files),
when in reality those files couldn't be opened because the device had
been unplugged.

The new method checks the adequacy of sysfs during libusb_init()
(op_init()) and if a sysfs file cannot be opened, it is now assumed that
it is because the device has been unplugged, not because sysfs is
inadequate.

Signed-off-by: Alan Ott <alan@signal11.us>
[stuge: Include closedir() bugfix posted in ticket by Arne Laansoo]
[stuge: Remove dead code in sysfs_scan_device() found by Hans de Goede]
libusb/os/linux_usbfs.c