-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler.c
507 lines (420 loc) · 13.5 KB
/
eventhandler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
*
* Copyright (C) 2000-2002
* David Corcoran <corcoran@musclecard.com>
* Copyright (C) 2002-2011
* Ludovic Rousseau <ludovic.rousseau@free.fr>
*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: eventhandler.c 7004 2014-10-02 09:26:36Z rousseau $
*/
/**
* @file
* @brief This keeps track of card insertion/removal events
* and updates ATR, protocol, and status information.
*/
#include <PCSC/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <PCSC/misc.h>
#include <PCSC/pcscd.h>
#include <PCSC/debuglog.h>
#include <PCSC/readerfactory.h>
#include <PCSC/eventhandler.h>
#include <PCSC/dyn_generic.h>
#include <PCSC/sys_generic.h>
#include <PCSC/ifdwrapper.h>
#include <PCSC/prothandler.h>
#include <PCSC/strlcpycat.h>
#include <PCSC/utils.h>
#include <PCSC/winscard_svc.h>
#include <PCSC/simclist.h>
static list_t ClientsWaitingForEvent; /**< list of client file descriptors */
pthread_mutex_t ClientsWaitingForEvent_lock; /**< lock for the above list */
static void EHStatusHandlerThread(READER_CONTEXT *);
LONG EHRegisterClientForEvent(int32_t filedes)
{
(void)pthread_mutex_lock(&ClientsWaitingForEvent_lock);
(void)list_append(&ClientsWaitingForEvent, &filedes);
(void)pthread_mutex_unlock(&ClientsWaitingForEvent_lock);
return SCARD_S_SUCCESS;
} /* EHRegisterClientForEvent */
/**
* Try to unregisted a client
* If no client is found then do not log an error
*/
LONG EHTryToUnregisterClientForEvent(int32_t filedes)
{
LONG rv = SCARD_S_SUCCESS;
int ret;
(void)pthread_mutex_lock(&ClientsWaitingForEvent_lock);
ret = list_delete(&ClientsWaitingForEvent, &filedes);
(void)pthread_mutex_unlock(&ClientsWaitingForEvent_lock);
if (ret < 0)
rv = SCARD_F_INTERNAL_ERROR;
return rv;
} /* EHTryToUnregisterClientForEvent */
/**
* Unregister a client and log an error if the client is not found
*/
LONG EHUnregisterClientForEvent(int32_t filedes)
{
LONG rv = EHTryToUnregisterClientForEvent(filedes);
if (rv < 0)
Log2(PCSC_LOG_ERROR, "Can't remove client: %d", filedes);
return rv;
} /* EHUnregisterClientForEvent */
/**
* Sends an asynchronous event to any waiting client
*/
LONG EHSignalEventToClients(void)
{
LONG rv = SCARD_S_SUCCESS;
int32_t filedes;
(void)pthread_mutex_lock(&ClientsWaitingForEvent_lock);
(void)list_iterator_start(&ClientsWaitingForEvent);
while (list_iterator_hasnext(&ClientsWaitingForEvent))
{
filedes = *(int32_t *)list_iterator_next(&ClientsWaitingForEvent);
rv = MSGSignalClient(filedes, SCARD_S_SUCCESS);
}
(void)list_iterator_stop(&ClientsWaitingForEvent);
(void)list_clear(&ClientsWaitingForEvent);
(void)pthread_mutex_unlock(&ClientsWaitingForEvent_lock);
return rv;
} /* EHSignalEventToClients */
LONG EHInitializeEventStructures(void)
{
(void)list_init(&ClientsWaitingForEvent);
/* request to store copies, and provide the metric function */
(void)list_attributes_copy(&ClientsWaitingForEvent, list_meter_int32_t, 1);
/* setting the comparator, so the list can sort, find the min, max etc */
(void)list_attributes_comparator(&ClientsWaitingForEvent, list_comparator_int32_t);
(void)pthread_mutex_init(&ClientsWaitingForEvent_lock, NULL);
return SCARD_S_SUCCESS;
}
LONG EHDeinitializeEventStructures(void)
{
list_destroy(&ClientsWaitingForEvent);
pthread_mutex_destroy(&ClientsWaitingForEvent_lock);
return SCARD_S_SUCCESS;
}
LONG EHDestroyEventHandler(READER_CONTEXT * rContext)
{
int rv;
DWORD dwGetSize;
UCHAR ucGetData[1];
if ('\0' == rContext->readerState->readerName[0])
{
Log1(PCSC_LOG_INFO, "Thread already stomped.");
return SCARD_S_SUCCESS;
}
/*
* Set the thread to 0 to exit thread
*/
rContext->hLockId = 0xFFFF;
Log1(PCSC_LOG_INFO, "Stomping thread.");
/* kill the "polling" thread */
dwGetSize = sizeof(ucGetData);
rv = IFDGetCapabilities(rContext, TAG_IFD_POLLING_THREAD_KILLABLE,
&dwGetSize, ucGetData);
#ifdef HAVE_PTHREAD_CANCEL
if ((IFD_SUCCESS == rv) && (1 == dwGetSize) && ucGetData[0])
{
Log1(PCSC_LOG_INFO, "Killing polling thread");
(void)pthread_cancel(rContext->pthThread);
}
else
#endif
{
/* ask to stop the "polling" thread */
RESPONSECODE (*fct)(DWORD) = NULL;
dwGetSize = sizeof(fct);
rv = IFDGetCapabilities(rContext, TAG_IFD_STOP_POLLING_THREAD,
&dwGetSize, (PUCHAR)&fct);
if ((IFD_SUCCESS == rv) && (dwGetSize == sizeof(fct)))
{
Log1(PCSC_LOG_INFO, "Request stopping of polling thread");
fct(rContext->slot);
}
else
Log1(PCSC_LOG_INFO, "Waiting polling thread");
}
/* wait for the thread to finish */
rv = pthread_join(rContext->pthThread, NULL);
if (rv)
Log2(PCSC_LOG_ERROR, "pthread_join failed: %s", strerror(rv));
/* Zero the thread */
rContext->pthThread = 0;
Log1(PCSC_LOG_INFO, "Thread stomped.");
return SCARD_S_SUCCESS;
}
LONG EHSpawnEventHandler(READER_CONTEXT * rContext)
{
LONG rv;
DWORD dwStatus = 0;
rv = IFDStatusICC(rContext, &dwStatus);
if (rv != SCARD_S_SUCCESS)
{
Log2(PCSC_LOG_ERROR, "Initial Check Failed on %s",
rContext->readerState->readerName);
return SCARD_F_UNKNOWN_ERROR;
}
rv = ThreadCreate(&rContext->pthThread, 0,
(PCSCLITE_THREAD_FUNCTION( ))EHStatusHandlerThread, (LPVOID) rContext);
if (rv)
{
Log2(PCSC_LOG_ERROR, "ThreadCreate failed: %s", strerror(rv));
return SCARD_E_NO_MEMORY;
}
else
return SCARD_S_SUCCESS;
}
static void EHStatusHandlerThread(READER_CONTEXT * rContext)
{
LONG rv;
const char *readerName;
DWORD dwStatus;
uint32_t readerState;
int32_t readerSharing;
DWORD dwCurrentState;
#ifndef DISABLE_AUTO_POWER_ON
DWORD dwAtrLen;
#endif
/*
* Zero out everything
*/
dwStatus = 0;
readerName = rContext->readerState->readerName;
rv = IFDStatusICC(rContext, &dwStatus);
if ((SCARD_S_SUCCESS == rv) && (dwStatus & SCARD_PRESENT))
{
#ifdef DISABLE_AUTO_POWER_ON
rContext->readerState->cardAtrLength = 0;
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
readerState = SCARD_PRESENT;
Log1(PCSC_LOG_INFO, "Skip card power on");
#else
dwAtrLen = sizeof(rContext->readerState->cardAtr);
rv = IFDPowerICC(rContext, IFD_POWER_UP,
rContext->readerState->cardAtr, &dwAtrLen);
rContext->readerState->cardAtrLength = dwAtrLen;
/* the protocol is unset after a power on */
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
if (rv == IFD_SUCCESS)
{
readerState = SCARD_PRESENT | SCARD_POWERED | SCARD_NEGOTIABLE;
rContext->powerState = POWER_STATE_POWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_POWERED");
if (rContext->readerState->cardAtrLength > 0)
{
LogXxd(PCSC_LOG_INFO, "Card ATR: ",
rContext->readerState->cardAtr,
rContext->readerState->cardAtrLength);
}
else
Log1(PCSC_LOG_INFO, "Card ATR: (NULL)");
}
else
{
readerState = SCARD_PRESENT | SCARD_SWALLOWED;
rContext->powerState = POWER_STATE_UNPOWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_UNPOWERED");
Log3(PCSC_LOG_ERROR, "Error powering up card: %ld 0x%04lX", rv, rv);
}
#endif
dwCurrentState = SCARD_PRESENT;
}
else
{
readerState = SCARD_ABSENT;
rContext->readerState->cardAtrLength = 0;
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
dwCurrentState = SCARD_ABSENT;
}
/*
* Set all the public attributes to this reader
*/
rContext->readerState->readerState = readerState;
rContext->readerState->readerSharing = readerSharing = rContext->contexts;
(void)EHSignalEventToClients();
while (1)
{
dwStatus = 0;
rv = IFDStatusICC(rContext, &dwStatus);
if (rv != SCARD_S_SUCCESS)
{
Log2(PCSC_LOG_ERROR, "Error communicating to: %s", readerName);
/*
* Set error status on this reader while errors occur
*/
rContext->readerState->readerState = SCARD_UNKNOWN;
rContext->readerState->cardAtrLength = 0;
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
dwCurrentState = SCARD_UNKNOWN;
(void)EHSignalEventToClients();
}
if (dwStatus & SCARD_ABSENT)
{
if (dwCurrentState == SCARD_PRESENT ||
dwCurrentState == SCARD_UNKNOWN)
{
/*
* Change the status structure
*/
Log2(PCSC_LOG_INFO, "Card Removed From %s", readerName);
/*
* Notify the card has been removed
*/
(void)RFSetReaderEventState(rContext, SCARD_REMOVED);
rContext->readerState->cardAtrLength = 0;
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
rContext->readerState->readerState = SCARD_ABSENT;
dwCurrentState = SCARD_ABSENT;
rContext->readerState->eventCounter++;
(void)EHSignalEventToClients();
}
}
else if (dwStatus & SCARD_PRESENT)
{
if (dwCurrentState == SCARD_ABSENT ||
dwCurrentState == SCARD_UNKNOWN)
{
#ifdef DISABLE_AUTO_POWER_ON
rContext->readerState->cardAtrLength = 0;
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
rContext->readerState->readerState = SCARD_PRESENT;
rContext->powerState = POWER_STATE_UNPOWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_UNPOWERED");
rv = IFD_SUCCESS;
Log1(PCSC_LOG_INFO, "Skip card power on");
#else
/*
* Power and reset the card
*/
dwAtrLen = sizeof(rContext->readerState->cardAtr);
rv = IFDPowerICC(rContext, IFD_POWER_UP,
rContext->readerState->cardAtr, &dwAtrLen);
rContext->readerState->cardAtrLength = dwAtrLen;
/* the protocol is unset after a power on */
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
if (rv == IFD_SUCCESS)
{
rContext->readerState->readerState = SCARD_PRESENT | SCARD_POWERED | SCARD_NEGOTIABLE;
rContext->powerState = POWER_STATE_POWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_POWERED");
}
else
{
rContext->readerState->readerState = SCARD_PRESENT | SCARD_SWALLOWED;
rContext->powerState = POWER_STATE_UNPOWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_UNPOWERED");
rContext->readerState->cardAtrLength = 0;
}
#endif
dwCurrentState = SCARD_PRESENT;
rContext->readerState->eventCounter++;
Log2(PCSC_LOG_INFO, "Card inserted into %s", readerName);
(void)EHSignalEventToClients();
if (rv == IFD_SUCCESS)
{
if (rContext->readerState->cardAtrLength > 0)
{
LogXxd(PCSC_LOG_INFO, "Card ATR: ",
rContext->readerState->cardAtr,
rContext->readerState->cardAtrLength);
}
else
Log1(PCSC_LOG_INFO, "Card ATR: (NULL)");
}
else
Log1(PCSC_LOG_ERROR,"Error powering up card.");
}
}
/*
* Sharing may change w/o an event pass it on
*/
if (readerSharing != rContext->contexts)
{
readerSharing = rContext->contexts;
rContext->readerState->readerSharing = readerSharing;
(void)EHSignalEventToClients();
}
if (rContext->pthCardEvent)
{
int ret;
int timeout;
#ifndef DISABLE_ON_DEMAND_POWER_ON
if (POWER_STATE_POWERED == rContext->powerState)
/* The card is powered but not yet used */
timeout = PCSCLITE_POWER_OFF_GRACE_PERIOD;
else
/* The card is already in use or not used at all */
#endif
timeout = PCSCLITE_STATUS_EVENT_TIMEOUT;
ret = rContext->pthCardEvent(rContext->slot, timeout);
if (IFD_SUCCESS != ret)
(void)SYS_USleep(PCSCLITE_STATUS_POLL_RATE);
}
else
(void)SYS_USleep(PCSCLITE_STATUS_POLL_RATE);
#ifndef DISABLE_ON_DEMAND_POWER_ON
/* the card is powered but not used */
(void)pthread_mutex_lock(&rContext->powerState_lock);
if (POWER_STATE_POWERED == rContext->powerState)
{
/* power down */
IFDPowerICC(rContext, IFD_POWER_DOWN, NULL, NULL);
rContext->powerState = POWER_STATE_UNPOWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_UNPOWERED");
/* the protocol is unset after a power down */
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
}
/* the card was in use */
if (POWER_STATE_GRACE_PERIOD == rContext->powerState)
{
/* the next state should be UNPOWERED unless the
* card is used again */
rContext->powerState = POWER_STATE_POWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_POWERED");
}
(void)pthread_mutex_unlock(&rContext->powerState_lock);
#endif
if (rContext->hLockId == 0xFFFF)
{
/*
* Exit and notify the caller
*/
(void)EHSignalEventToClients();
Log1(PCSC_LOG_INFO, "Die");
rContext->hLockId = 0;
(void)pthread_exit(NULL);
}
}
}