1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-09 23:39:51 +02:00

scd: Fix possible race for watching thread doesn't exit.

* scd/apdu.c (release_pcsc_context): Synchronize the exit.
(pcsc_init): Use joinable thread.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2021-05-14 12:38:06 +09:00
parent f2f9628006
commit 8640f66b90

View File

@ -85,6 +85,7 @@ typedef unsigned long pcsc_dword_t;
/* PC/SC context to access readers. Shared among all readers. */ /* PC/SC context to access readers. Shared among all readers. */
static struct pcsc_global_data { static struct pcsc_global_data {
HANDLE context; HANDLE context;
npth_t thread;
int count; int count;
const char *rdrname[MAX_READER]; const char *rdrname[MAX_READER];
} pcsc; } pcsc;
@ -786,10 +787,19 @@ control_pcsc (int slot, pcsc_dword_t ioctl_code,
static void static void
release_pcsc_context (void) release_pcsc_context (void)
{ {
int err;
/*log_debug ("%s: releasing context\n", __func__);*/ /*log_debug ("%s: releasing context\n", __func__);*/
log_assert (pcsc.context != 0); log_assert (pcsc.context != 0);
pcsc_release_context (pcsc.context); pcsc_release_context (pcsc.context);
pcsc.context = 0; pcsc.context = 0;
npth_mutex_unlock (&reader_table_lock);
err = npth_join (pcsc.thread, NULL);
if (err)
log_error ("release_pcsc_context: error joining thread: %s\n", strerror (err));
pcsc.thread = 0;
npth_mutex_lock (&reader_table_lock);
} }
static int static int
@ -1273,7 +1283,6 @@ pcsc_init (void)
} }
else else
{ {
npth_t thread;
npth_attr_t tattr; npth_attr_t tattr;
int err_npth; int err_npth;
@ -1284,8 +1293,8 @@ pcsc_init (void)
return -1; return -1;
} }
npth_attr_setdetachstate (&tattr, NPTH_CREATE_DETACHED); npth_attr_setdetachstate (&tattr, NPTH_CREATE_JOINABLE);
err_npth = npth_create (&thread, &tattr, pcsc_thread, NULL); err_npth = npth_create (&pcsc.thread, &tattr, pcsc_thread, NULL);
if (err_npth) if (err_npth)
{ {
log_error ("npth_create failed: %s\n", strerror (err_npth)); log_error ("npth_create failed: %s\n", strerror (err_npth));