From bc4eeda5737dbd74f7c48d437f70c4f9c927406f Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 1 Mar 2019 15:46:09 +0100 Subject: [PATCH] Remove readers when smart card service stops The code already removes all active cards when the service goes away, but it doesn't remove the reader. This can be a bit confusing since they will still be polled and listed. --- src/libopensc/reader-pcsc.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libopensc/reader-pcsc.c b/src/libopensc/reader-pcsc.c index 94285448..a058c71f 100644 --- a/src/libopensc/reader-pcsc.c +++ b/src/libopensc/reader-pcsc.c @@ -1351,6 +1351,29 @@ static int pcsc_detect_readers(sc_context_t *ctx) } else { rv = gpriv->SCardListReaders(gpriv->pcsc_ctx, NULL, NULL, (LPDWORD) &reader_buf_size); + + /* + * All readers have disappeared, so mark them as + * such so we don't keep polling them over and over. + */ + if ( +#ifdef SCARD_E_NO_READERS_AVAILABLE + (rv == (LONG)SCARD_E_NO_READERS_AVAILABLE) || +#endif + (rv == (LONG)SCARD_E_NO_SERVICE) || (rv == (LONG)SCARD_E_SERVICE_STOPPED)) { + + for (i = 0; i < sc_ctx_get_reader_count(ctx); i++) { + sc_reader_t *reader = sc_ctx_get_reader(ctx, i); + + if (!reader) { + ret = SC_ERROR_INTERNAL; + goto out; + } + + reader->flags |= SC_READER_REMOVED; + } + } + if ((rv == (LONG)SCARD_E_NO_SERVICE) || (rv == (LONG)SCARD_E_SERVICE_STOPPED)) { gpriv->SCardReleaseContext(gpriv->pcsc_ctx); gpriv->pcsc_ctx = 0;