diff --git a/scd/apdu.h b/scd/apdu.h --- a/scd/apdu.h +++ b/scd/apdu.h @@ -98,6 +98,7 @@ #define APDU_CARD_PRESENT (2) /* Card is just present. */ #define APDU_CARD_ACTIVE (4) /* Card is active. */ +extern npth_mutex_t reader_table_lock; gpg_error_t apdu_init (void); diff --git a/scd/apdu.c b/scd/apdu.c --- a/scd/apdu.c +++ b/scd/apdu.c @@ -147,7 +147,7 @@ static struct reader_table_s reader_table[MAX_READER]; #ifdef USE_NPTH -static npth_mutex_t reader_table_lock; +npth_mutex_t reader_table_lock; #endif @@ -1992,8 +1992,6 @@ dl->idx = 0; dl->idx_max = 0; - npth_mutex_lock (&reader_table_lock); - #ifdef HAVE_LIBUSB if (!opt.disable_ccid) { @@ -2001,7 +1999,6 @@ if (err) { xfree (dl); - npth_mutex_unlock (&reader_table_lock); return err; } @@ -2011,7 +2008,6 @@ log_debug ("leave: apdu_open_reader => slot=-1 (no ccid)\n"); xfree (dl); - npth_mutex_unlock (&reader_table_lock); return gpg_error (GPG_ERR_ENODEV); } } @@ -2028,7 +2024,6 @@ if (pcsc_init () < 0) { xfree (dl); - npth_mutex_unlock (&reader_table_lock); return gpg_error (GPG_ERR_NO_SERVICE); } } @@ -2044,7 +2039,6 @@ log_error ("error allocating memory for reader list\n"); if (pcsc.count == 0) release_pcsc_context (); - npth_mutex_unlock (&reader_table_lock); xfree (dl); return err; } @@ -2057,7 +2051,6 @@ xfree (p); if (pcsc.count == 0) release_pcsc_context (); - npth_mutex_unlock (&reader_table_lock); xfree (dl); return iso7816_map_sw (pcsc_error_to_sw (r)); } @@ -2094,12 +2087,8 @@ break; } } - - pcsc.count++; } - npth_mutex_unlock (&reader_table_lock); - *l_p = dl; return 0; } @@ -2123,10 +2112,11 @@ pcsc.rdrname[i] = NULL; log_assert (pcsc.count > 0); - if (--pcsc.count == 0) + if (pcsc.count == 0) release_pcsc_context (); } xfree (dl); + npth_mutex_unlock (&reader_table_lock); } diff --git a/scd/app.c b/scd/app.c --- a/scd/app.c +++ b/scd/app.c @@ -715,9 +715,11 @@ int new_card = 0; /* Scan the devices to find new device(s). */ + npth_mutex_lock (&reader_table_lock); err = apdu_dev_list_start (opt.reader_port, &l); if (err) { + npth_mutex_unlock (&reader_table_lock); npth_mutex_unlock (&card_list_lock); return err; } @@ -747,11 +749,14 @@ if (err) { pincache_put (ctrl, slot, NULL, NULL, NULL, 0); + npth_mutex_unlock (&reader_table_lock); apdu_close_reader (slot); + npth_mutex_lock (&reader_table_lock); } } apdu_dev_list_finish (l); + npth_mutex_unlock (&reader_table_lock); /* If new device(s), kick the scdaemon loop. */ if (new_card)