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