<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-04-20 18:57 GMT+02:00 Maksim Ivanov <span dir="ltr"><<a href="mailto:emaxx@google.com" target="_blank">emaxx@google.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello,<br></blockquote><div><br></div><div>Hello,<br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Looking at the CCID source code, it seems that there may be some<br>
problems with working with multiple readers simultaneously.<br>
<br>
The CCID reader polling code (or, to be precise, its libusb-based<br>
version) is based on the libusb_handle_events function:<br>
<a href="https://alioth.debian.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=pcsclite/CCID.git;a=blob;f=src/ccid_usb.c;h=6a097eadb821ebed39eefac6c4b39b3f8f135001#l1252" rel="noreferrer" target="_blank">https://alioth.debian.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=pcsclite/CCID.git;a=blob;f=src/ccid_usb.c;h=6a097eadb821ebed39eefac6c4b39b3f8f135001#l1252</a><br>
<br>
The problem with this function is that, as it's documented in libusb<br>
docs, it's prone to race conditions:<br>
<a href="http://libusb.sourceforge.net/api-1.0/mtasync.html#Using" rel="noreferrer" target="_blank">http://libusb.sourceforge.net/api-1.0/mtasync.html#Using</a><br>
<br>
Seems that the races are pretty likely to occur when there are two or<br>
more readers operated simultaneously. For each of them, a thread with<br>
a polling transfer is created by the CCID driver; each thread is<br>
blocked on libusb_handle_events most of its time. The first type of<br>
race may happen between checking the "completed" variable and calling<br>
the libusb_handle_events function: another thread with the running<br>
libusb_handle_events function may finish this transfer during this<br>
period of time. The second type of race roots from the fact that there<br>
is no guarantee which thread's libusb_handle_event will catch the<br>
events, so it may be that one polling thread misses the transfer<br>
completion it's waiting for.<br>
<br>
Even though the races, when they occur, result in just 60 seconds<br>
delay, it would be great if they would be omitted completely.<br>
The fix shouldn't be difficult, based on the handy function<br>
libusb_handle_events_completed, which appears to be solving exactly<br>
this problem:<br>
<a href="http://libusb.sourceforge.net/api-1.0/group__poll.html#ga0bc99f39e4cf5ad393cd5936c36037d1" rel="noreferrer" target="_blank">http://libusb.sourceforge.net/api-1.0/group__poll.html#ga0bc99f39e4cf5ad393cd5936c36037d1</a><br></blockquote></div><br><br></div><div class="gmail_extra">Fixed in <a href="https://github.com/LudovicRousseau/CCID/commit/2c8ffab9962484653b6445a6c85a6c690a5948b3">https://github.com/LudovicRousseau/CCID/commit/2c8ffab9962484653b6445a6c85a6c690a5948b3</a><br><br></div><div class="gmail_extra">Thanks<br clear="all"></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature"> Dr. Ludovic Rousseau</div>
</div></div>