Commit Graph

806 Commits

Author SHA1 Message Date
Nathan Hjelm b50a433c16 linux: ensure the netlink socket is correctly opened with older Linux kernels
It is possible for the extra socket flags (available from 2.6.27 on) to be
defined but not available. Check for this case and set the flags correctly
on the netlink socket.
2013-08-07 21:01:43 -06:00
Chris Dickens c848e5b72a Core: Fix potential segfault caused by using freed memory
When a transfer is submitted, the device is referenced in libusb_submit_transfer()
and unreferenced in usbi_handle_transfer_completion(). This transfer could potentially
be freed by any user callback, or is freed by libusb if LIBUSB_TRANSFER_FREE_TRANSFER
is set in the flags. The call to unreference the device uses this potentially freed
memory. Reading the device handle beforehand will prevent this disaster.
2013-08-07 20:45:43 -06:00
Simon Haggett 69d88b7fef Windows: Fix race between windows_clock_gettime_threaded() and windows_clock_gettime()
* When the timer thread is created in windows_init(), it performs an
  initialisation phase in which it uses QueryPerformanceFrequency() to determine
  if there is a high resolution timer available, and sets hires_frequency and
  hires_ticks_to_ps appropriately. However, since windows_init() does not wait for
  this initialisation phase to complete, windows_clock_gettime() can be called
  before hires_frequency and hires_ticks_to_ps have been updated. This can result
  in windows_clock_gettime() temporarily returning real-time clock values even
  though the platform supports a monotonic clock.
* See http://sourceforge.net/mailarchive/forum.php?thread_name=1373620013-3574-1-git-send-email-simon.haggett%40realvnc.com&forum_name=libusbx-devel
2013-08-08 00:43:57 +01:00
Simon Haggett b3562727e9 WinCE: Fix race between wince_clock_gettime_threaded() and wince_clock_gettime()
* When the timer thread is created in wince_init(), it performs an initialisation
  phase in which it uses QueryPerformanceFrequency() to determine if there is a
  high resolution timer available, and sets hires_frequency and hires_ticks_to_ps
  appropriately. However, since wince_init() does not wait for this initialisation
  phase to complete, wince_clock_gettime() can be called before hires_frequency
  and hires_ticks_to_ps have been updated. This can result in
  wince_clock_gettime() temporarily returning real-time clock values even though
  the platform supports a monotonic clock.
* See http://sourceforge.net/mailarchive/forum.php?thread_name=1373619997-3535-1-git-send-email-simon.haggett%40realvnc.com&forum_name=libusbx-devel
2013-08-08 00:41:46 +01:00
Simon Haggett 388a9a8805 Windows: fd_to_winfd() shouldn't treat fd 0 as invalid
* fd_to_winfd() currently returns INVALID_WINFD if fd is 0, but usbi_create_fd()
  can legally assign an fd number of 0 if poll_fd[0] is not already occupied.
* Transfers which are assigned an fd number of 0 for their event handle are then
  unable to have their event handle retrieved later on.

See http://libusbx.1081486.n5.nabble.com/Libusbx-devel-PATCH-1-1-Windows-fd-to-winfd-shouldn-t-treat-fd-0-as-invalid-tt1535.html
2013-08-08 00:37:30 +01:00
Sean McBride a3b92f8fe1 README: Add OS X specific notes
Xcode 4.3 do not provide autotools anymore.
libusbx now provides a Xcode project to ease build Mac OS X.
2013-08-02 11:40:33 +02:00
Chris Dickens 7dad81fe61 Core: correctly check usbi_read() returned value
For messages received on the hotplug pipe, the message was read via
usbi_read() (ssize_t) and compared against the size of the message
struct (size_t). usbi_read() returns -1 on an error condition, so some
systems can cast the ssize_t to size_t for the comparison, making it
equal to SIZE_MAX and causing the error check condition to incorrectly
evaluate to false.
2013-08-02 11:37:21 +02:00
Sean McBride fedc3631f8 Core: defensive programming
Defensively set return-by-reference value to -1 in error condition

NB: The comments do not match the implementation.

Comments: "[return] the index of the configuration matching a specific
bConfigurationValue in the idx output parameter, or -1 if the config was
not found"

There is a code path where idx is never touched. Perhaps clients of the
function are careful to only read idx if the return value is success,
but also setting idx to -1 is much safer.
2013-08-02 11:30:16 +02:00
hjelmn b5acea3591 darwin: fix clang warnings about explicit conversion
There shouldn't be any problems with any supported version of OSX in
converting a UInt64 to an unsigned long. They should be the same size
but even if they are not the session should still be unique.
2013-07-30 13:33:01 -06:00
Sean McBride 4cb9ae0c22 fixed link errors by adding missing files to Xcode project 2013-07-30 10:44:21 -06:00
Sean McBride ce8da723b4 made some globals static to fix warnings 2013-07-30 10:44:13 -06:00
Sean McBride ea6c827af0 fixed some clang -Wdocumentation warnings from bad doxygen markup 2013-07-30 10:44:02 -06:00
Sean McBride 4d198fe0cf fixed compiler warning about possible uninitialized use by zero initializing 2013-07-30 10:43:49 -06:00
Sean McBride 8f13c72409 make targets for examples depend on library target. Generate library named libusb-1.0.0.dylib not libusbx.dylib. Added several xcconfig files to make source control and documentation easier. Reorganised a few files' positions within the project. Disable strict aliasing since libusbx breaks strict aliasing rules anyway. Set project format as 3.1-compatible.
Conflicts:
	Xcode/libusbx.xcodeproj/project.pbxproj
2013-07-30 10:43:34 -06:00
Ludovic Rousseau 059e648ab9 Xcode: build for 32 and 64 bits 2013-07-30 10:42:48 -06:00
Ludovic Rousseau bd57a42aba Xcode: remove implicit/default configuration 2013-07-30 10:42:31 -06:00
Ludovic Rousseau a0d7a89c33 Add minimal config.h for Xcode
Xcode do not use ./configure so the config.h file needs to be generated
by hand.
This config.h is the minimal file for libusbx built using Xcode.
2013-07-30 10:41:44 -06:00
Ludovic Rousseau b11f8e08e0 Adapt Xcode project to the new paths 2013-07-30 10:41:24 -06:00
Ludovic Rousseau 1640bf7f95 Move Xcode project in Xcode/ directory
This is to use Xcode special config.h file in a independent directory.
The same is already done for MS Visual C with msvc/ directory.
2013-07-30 10:40:52 -06:00
Ludovic Rousseau db59090457 Xcode project 2013-07-30 10:40:35 -06:00
Hans de Goede 3107f30bad linux_netlink: Remove use of pthread_cancel
Using pthread_cancel() presents the opportunity for deadlock, so
use a control pipe to cause the event thread to gracefully exit.

Inspired on the identical patch for linux_udev from Chris Dickens.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-30 17:21:20 +02:00
Hans de Goede 9afce4282e linux_netlink: close netlink socket on init error
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-30 17:21:08 +02:00
Chris Dickens 6ac8cd3ef3 hotplug: Remove use of pthread_cancel from linux_udev
Using pthread_cancel() presents the opportunity for deadlock, so
use a control pipe to cause the event thread to gracefully exit.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-30 17:20:39 +02:00
Hans de Goede d8a33df7dd linux: Use a separate lock to serialize start/stop vs hotplug events
Using one lock for this is a bad idea, as we should not be holding any
locks used by the hotplug thread when trying to stop otherwise the stop
function may wait indefinetely in pthread_join, while the event-thread
is waiting for the lock the caller of the stop function holds.

Using 2 separate locks for this should fix this deadlock, which has been
reported here: https://bugzilla.redhat.com/show_bug.cgi?id=985484

Many thanks to Chris Dickens for figuring out the cause of this deadlock!

CC: Chris Dickens <christopher.a.dickens@gmail.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-30 17:20:27 +02:00
hjelmn 242d49c636 work around Linux systems that don't provide SOCK_CLOEXEC or SOCK_NONBLOCK
These options were added in 2.6.27 and are not available on all kernels
that support netlink. Set these options using fcntl when SOCK_CLOEXEC and
SOCK_NONBLOCK are not available.

Closes #124.
2013-07-30 09:10:49 -06:00
hjelmn 252e193d9a fix warnings in linux_get_device_address
Warnings:

os/linux_usbfs.c: In function 'linux_get_device_address':
os/linux_usbfs.c:620: warning: comparison is always false due to limited range of data type
os/linux_usbfs.c:624: warning: comparison is always false due to limited range of data type
os/linux_usbfs.c:628: warning: comparison is always false due to limited range of data type
os/linux_usbfs.c:628: warning: comparison is always false due to limited range of data type
2013-07-30 09:10:48 -06:00
hjelmn 67f0c1f100 include stdlib.h for free and realloc in libusbi.h
References #124.
2013-07-30 09:10:36 -06:00
Nathan Hjelm 872ff1704b Silence automake 1.14 warning
The warning is:

libusb/Makefile.am:57: warning: source file 'os/threads_windows.c' is in a subdirectory,
libusb/Makefile.am:57: but option 'subdir-objects' is disabled
automake: warning: possible forward-incompatibility.
automake: At least a source file is in a subdirectory, but the 'subdir-objects'
automake: automake option hasn't been enabled.  For now, the corresponding output
automake: object file(s) will be placed in the top-level directory.  However,
automake: this behaviour will change in future Automake versions: they will
automake: unconditionally cause object files to be placed in the same subdirectory
automake: of the corresponding sources.
automake: You are advised to start using 'subdir-objects' option throughout your
automake: project, to avoid future incompatibilities.

Fixed by setting the subdir-objects option.

Closes #125.
2013-07-30 09:10:19 -06:00
Nathan Hjelm 83021abc20 Add checks for headers needed by linux/netlink.h
These headers are required by netlink.h. I am not sure how this
worked at all. It certainly doesn't work with older versions of Linux
2.6.

References #124.
2013-07-30 09:10:08 -06:00
Nathan Hjelm 707d500b9f keep a reference to the device for each active transfer and let the backend handle cancelling active transfers when a device is disconnected
This commit should fix issues with active transfers when a device is
disconnected. The backend is responsible for making sure the completion
callbacks are made, not the hotplug code. This should fix a number of
issues including duplicate callbacks and segmentation faults.

References #124.
2013-07-30 09:08:39 -06:00
Florian Albrechtskirchinger 97958ba756 hotplug: Pass explicit context to callbacks
* Instead of passing NULL for the context to hotplug callbacks, if the
  context happens to be the default context, always pass the explicit
  context pointer.
2013-07-23 20:23:13 +01:00
Hans de Goede d1ed6c4d67 Prepare for 1.0.16 final release
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
v1.0.16
2013-07-11 11:04:02 +02:00
Hans de Goede faa62417fb Prepare for 1.0.16-rc3 release
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
v1.0.16-rc3
2013-07-06 09:09:25 +02:00
Hans de Goede ef59b06f4a Documentation: add an Using an event handling thread section
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-05 16:57:51 +02:00
Hans de Goede 230b9a0105 Documentation: explain the 2 main viable event handling approaches
Stop pretending that having a separate event handling thread is a bad thing,
specifically delete the "[this] option is not very nice either, but may be
the nicest option available to you if the "proper" approach can not be applied
to your application", which suggests that using poll integration into a main
loop is the one and only "proper" approach.

Instead clearly document there are 2 viable approaches, using a separate
thread, or poll integration into a main loop.

Also stop claiming that libusb does not use threads internally, as with
the new hotplug support this is no longer true.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-05 16:17:45 +02:00
Hans de Goede ac41bfc0fd hotplug: Wakeup libusb_handle_events on libusb_hotplug_deregister_callback
This serves 2 purposes:

1) We use lazy free-ing of the callback structure, for it to be actually
free-ed usbi_hotplug_match() needs to be called. This ensures this actually
happens (rather then waiting for a hotplug event to arrive, and not freeing
the callback as long as no such event arrives).

2) It causes libusb_handle_events to return to its caller on a call to
libusb_hotplug_deregister_callback, which is very useful for apps which use
a thread to do their apps (hotplug) event handling, otherwise that thread will
hang when the app tries to stop until some event happens.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-05 09:06:21 +02:00
Hans de Goede f505929798 hotplug: Give the usbi_hotplug_match* functions a context parameter
So that the device parameter can be NULL, in combination with a 0
events parameter, to be used to force lazy deregistration.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-05 08:38:13 +02:00
Pete Batard b4c18fac65 Doc: update hotplug, topology and descriptor documentation
* Also fix some typos
* Closes #95
2013-07-04 18:39:22 +01:00
Hans de Goede ea39a14d0a Prepare for 1.0.16-rc2 release
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
v1.0.16-rc2
2013-07-01 13:00:43 +02:00
Tim Roberts 975c4fd625 Core: Use fputs(3) instead of fprintf(3)
This is a micro-optimisation, but it should make the code easier to understand.
2013-06-30 21:14:24 +01:00
Pete Batard 7b893cc7ce Core: Fix writing of log lines a single fprintf call
* fb4c208c33 broke cygwin compilation due to __GCC__ not being defined
* The actual issue is that __GNUC__ rather than __GCC__ should have been used all along
* Also fixes gettimeofday() usage for MinGW/Cygwin
* Also increase log buffer size to 1K, fix a broken fprintf in core.c and sort whitespaces
2013-06-30 00:32:10 +01:00
Toby Gray fb4c208c33 Core: Make writing of log lines a single fprintf call.
Prior to this change a single line of logging performing several fprintf.

This change gets all the data for a line to be logged in a single
fprintf call. This reduced the chances of writes from another thread
getting intermixed with a log line.

It also makes it easier to change where logs are output to in the future.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-27 17:10:38 +02:00
Hans de Goede e2c54d93b1 core: Always warn when there are leaked device refs on exit
This check should done with hotplug capable backends too.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-27 09:33:28 +02:00
Toby Gray 004c7a7fab Core: Avoid passing uninitialised data down the hotplug pipe.
Due to alignment requirements, libusb_hotplug_message might have
some padding bytes.

This change makes sure that these padding bytes are
initialised. Valgrind no longer complains about passing uninitialised
data to the write system call.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-26 17:29:36 +02:00
Chris Dickens 83c9c3cb73 core.c: Initialize auto_detach_kernel_driver to 0 for new handle
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-26 17:24:36 +02:00
Hans de Goede 17caf6d179 openbsd: Fix memleak in obsd_get_device_list()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-26 17:13:44 +02:00
Hans de Goede 163e2083e2 core: Only do hotplug cleanup for hotplug capable backends
Xiaofan encountered a crash while testing on openbsd. The main problem here is
libusb_exit doing hotplug cleanup on a non hotplug capable backend.

If the usb_devs list is non empty (*) at libusb_exit time with a non hotplug
capable backend, then the hotplug cleanup code will unref the devices
in the list. Assuming this is the last unref, then libusb_unref_device
will call usbi_disconnect_device, which will try to take the usb_devs_lock,
which is already hold by libusb_exit. Note that if this deadlock was not
there, that we then also would have a double list_del issue.

*) This should never happen, if it does either libusb or the app has a memleak,
or the app still holds a reference to the device. The latter is an application
bug, since device->ctx will be invalid after libusb_exit, so the application
should not hold references after calling libusb_exit.

In this case we have a memleak the libusb openbsd code causing the usb_devs
list to be non empty. This will be fixed in another commit.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-26 17:11:27 +02:00
Hans de Goede c1bf7fbab0 WinCE: ref result of usbi_get_device_by_session_id()
After the "WinCE: Fix device reference leak which caused crash on
libusb_exit()" commit, the code always unref-s dev after adding it to
discovered_devs. But if dev comes from usbi_get_device_by_session_id()
it is a weak ref and as such should not be unreffed.

Instead of re-adding comlicate ref tracking logic, this patch fixes this with
a libusb_device_ref(dev) of dev comes from usbi_get_device_by_session_id(),
turning the weak ref into a strong ref.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-26 16:41:22 +02:00
Toby Gray 43fbe3df02 WinCE: Fix device reference leak which caused crash on libusb_exit().
The Windows CE device allocation code has always had a bug where it would
leak references to devices when they are allocated. This commit removes the
reference leak.

This leak was highlighted by the new hotplug code which now triggers a NULL
pointer dereference if not all devices are unreferenced before libusb_exit
is called.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-06-26 15:29:13 +02:00
Pete Batard 1b3c8272d6 Misc: Update AUTHORS file 2013-06-20 23:00:34 +01:00