From 314a31f8675a5901b18e866c66d4f021f06f746c Mon Sep 17 00:00:00 2001 From: martin Date: Wed, 3 Feb 2010 15:46:44 +0000 Subject: [PATCH] [PC/SC / MacOSX] Try to connect to the card twice. On OS X, when you insert a card, securityd sequentially starts all found Tokend-s to see if a card can be handled with one. If a non-tokend application waits for a card insertion with sc_wait_for_event and tries to connect to the card right after the system sees it, it will fail with "The reader is in use by another application" 95% of the time. With this hack connecting to the card succeeds 95% of the time with the probable penalty of an extra second on initialization for non-tokend clients. This should only affect applications that wait for card insertion events. git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3991 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/libopensc/reader-pcsc.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libopensc/reader-pcsc.c b/src/libopensc/reader-pcsc.c index c2337a7d..3359e621 100644 --- a/src/libopensc/reader-pcsc.c +++ b/src/libopensc/reader-pcsc.c @@ -414,6 +414,14 @@ static int pcsc_connect(sc_reader_t *reader) rv = priv->gpriv->SCardConnect(priv->gpriv->pcsc_ctx, reader->name, priv->gpriv->connect_exclusive ? SCARD_SHARE_EXCLUSIVE : SCARD_SHARE_SHARED, SCARD_PROTOCOL_ANY, &card_handle, &active_proto); +#ifdef __APPLE__ + if (rv == (LONG)SCARD_E_SHARING_VIOLATION) { + sleep(1); /* Try again to compete with Tokend probes */ + rv = priv->gpriv->SCardConnect(priv->gpriv->pcsc_ctx, reader->name, + priv->gpriv->connect_exclusive ? SCARD_SHARE_EXCLUSIVE : SCARD_SHARE_SHARED, + SCARD_PROTOCOL_ANY, &card_handle, &active_proto); + } +#endif if (rv != SCARD_S_SUCCESS) { PCSC_TRACE(reader, "SCardConnect failed", rv); return pcsc_to_opensc_error(rv);