Saturday, March 19. 2011
Friday Night Fever (or How a Computer Guy Spends the Friday Night)
For some strange reasons this week I needed to use my Spanish eID (DNIe) again and, sometimes these things happen, firefox hanged like crazy. Finally I had to reboot my system to use the platform pre-installed windows to complete the formality. Yesterday night I decided to find out what the hell was going on here.
As always I put opensc in debug mode (setting debug to 10 in /etc/opensc/opensc.conf) and what I saw was really amazing. Starting firefox (iceweasel) inside a terminal the debug dumped like crazy but it sometimes hanged for exactly 60 seconds. So it was not totally frozen, it worked quite well but sometimes it was waiting for something one minute. The problem is opensc calls a lot to the card and firefox hangs at least two or three times every startup (which means more than two or three minutes waiting firefox to come up).
First thing to always check in these situations is what has been changed (I have a thing about upgrading my Debian laptop). Since the time I wrote the DNIe entry the opensc packages (the project that deals with PKCS#11 and PKCS#15) remain the same, PC/SC lite (the project for communicating with smartcards and readers) has been upgraded from 1.5.5 to 1.6.6 and libccid (the handler library for my reader) from 1.3.11 to 1.4.2. I decided to come back to original packages (uninstalling and installing the old ones) to see what happened and, voilĂ , it started to work as before, no watings.
At that moment I was not totally happy cos I do not feel comfortable when I do not know what really happens and, even less, holding packages. So I started compiling by myself the complete software stack, first PC/SC lite 1.7.0, then libccid 1.4.2 and then opensc 0.12.1. But with opensc I changed my mind and I compiled it twice. First the old source from DNIe official page (opensc-dnie), the external library I talked about in the old entry, and opendnie (see this mail and this spanish blog), the effort of a Spanish guy to re-implement all the stuff to include Spanish eID into opensc without legal issues (maybe this work deserves a new entry). I had been thinking about opendnie for some time but I had never had the time to test it. The old source code for opensc-dnie now does not compile, the problem is libassuan (the little window the library shows when you are about to sign something) has changed its api. But checking opensc patch the modification of dialog.c was easy (here it is the patch if you want to use it). Opendnie compiled with no issues.
Testing and debugging I realized the problem was not in any of these packages but in libusb (the spurious hang also happened in my compiled versions and it did not matter you use opendnie or opensc-dnie). Now libccid 1.4.x is linked against libusb-1.0 (thing that did not happen with 1.3.x) and this is the reason the bug came out after the upgrade. So I also compiled libusb 1.0.8 and I saw the problem was inside libusb_control_transfer function. This function sends the transfer to the usb device and then waits for the response with a 60 seconds timeout. Finally I changed the fixed 60 seconds and tried again. And it worked, both solutions (opendnie and opensc-dnie) hanged no more. My patch was very crappy and straight forward but I opened a bug against libusb. At that time it was around 2 am at night and I had not even dinner. You know... it was a computering Friday night fever!
Today in the morning Ludovic Rousseau (the PC/SC and CCID guy) answered me saying this bug was known. He opened a ticket six months ago with a resolution patch inside but it is not closed yet (and Ludovic seems not very happy with that). I sent another post saying my issue can therefore be closed and then I tested Ludovic's patch. The patched libusb also worked correctly so I sent a final comment to Ludovic's ticket saying his patch fixed my issue (at this moment both posts are not displayed cos libusb moderates the comments). And that is all. I do not know how other jobs are but we are masochists and (worst of all) we enjoy it.
See you next time!
Comments