forked from LudovicRousseau/PCSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotplug_libudev.c
792 lines (660 loc) · 19.2 KB
/
hotplug_libudev.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
* MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
*
* Copyright (C) 2011
* Ludovic Rousseau <[email protected]>
* Copyright (C) 2014
* Stefani Seibold <[email protected]>
*
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.
*/
/**
* @file
* @brief This provides a search API for hot plugable devices using libudev
*/
#include "config.h"
#if defined(HAVE_LIBUDEV) && defined(USE_USB)
#define _GNU_SOURCE /* for asprintf(3) */
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <pthread.h>
#include <libudev.h>
#include <poll.h>
#include "debuglog.h"
#include "parser.h"
#include "readerfactory.h"
#include "sys_generic.h"
#include "hotplug.h"
#include "utils.h"
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(expression) \
(__extension__ \
({ long int __result; \
do __result = (long int) (expression); \
while (__result == -1L && errno == EINTR); \
__result; }))
#endif
#undef DEBUG_HOTPLUG
#define FALSE 0
#define TRUE 1
extern char Add_Interface_In_Name;
extern char Add_Serial_In_Name;
static pthread_t usbNotifyThread;
static int driverSize = -1;
static struct udev *Udev;
/**
* keep track of drivers in a dynamically allocated array
*/
static struct _driverTracker
{
unsigned int manuID;
unsigned int productID;
char *bundleName;
char *libraryPath;
char *readerName;
char *CFBundleName;
} *driverTracker = NULL;
#define DRIVER_TRACKER_SIZE_STEP 10
/* The CCID driver already supports 176 readers.
* We start with a big array size to avoid reallocation. */
#define DRIVER_TRACKER_INITIAL_SIZE 200
/**
* keep track of PCSCLITE_MAX_READERS_CONTEXTS simultaneous readers
*/
static struct _readerTracker
{
char *devpath; /**< device name seen by udev */
char *fullName; /**< full reader name (including serial number) */
char *sysname; /**< sysfs path */
} readerTracker[PCSCLITE_MAX_READERS_CONTEXTS];
static LONG HPReadBundleValues(void)
{
LONG rv;
DIR *hpDir;
struct dirent *currFP = NULL;
char fullPath[FILENAME_MAX];
char fullLibPath[FILENAME_MAX];
int listCount = 0;
hpDir = opendir(PCSCLITE_HP_DROPDIR);
if (NULL == hpDir)
{
Log1(PCSC_LOG_ERROR, "Cannot open PC/SC drivers directory: " PCSCLITE_HP_DROPDIR);
Log1(PCSC_LOG_ERROR, "Disabling USB support for pcscd.");
return -1;
}
/* allocate a first array */
driverSize = DRIVER_TRACKER_INITIAL_SIZE;
driverTracker = calloc(driverSize, sizeof(*driverTracker));
if (NULL == driverTracker)
{
Log1(PCSC_LOG_CRITICAL, "Not enough memory");
(void)closedir(hpDir);
return -1;
}
#define GET_KEY(key, values) \
rv = LTPBundleFindValueWithKey(&plist, key, values); \
if (rv) \
{ \
Log2(PCSC_LOG_ERROR, "Value/Key not defined for " key " in %s", \
fullPath); \
continue; \
}
while ((currFP = readdir(hpDir)) != 0)
{
if (strstr(currFP->d_name, ".bundle") != 0)
{
unsigned int alias;
list_t plist, *values;
list_t *manuIDs, *productIDs, *readerNames;
char *CFBundleName;
char *libraryPath;
/*
* The bundle exists - let's form a full path name and get the
* vendor and product ID's for this particular bundle
*/
(void)snprintf(fullPath, sizeof(fullPath), "%s/%s/Contents/Info.plist",
PCSCLITE_HP_DROPDIR, currFP->d_name);
fullPath[sizeof(fullPath) - 1] = '\0';
rv = bundleParse(fullPath, &plist);
if (rv)
continue;
/* get CFBundleExecutable */
GET_KEY(PCSCLITE_HP_LIBRKEY_NAME, &values)
libraryPath = list_get_at(values, 0);
(void)snprintf(fullLibPath, sizeof(fullLibPath),
"%s/%s/Contents/%s/%s",
PCSCLITE_HP_DROPDIR, currFP->d_name, PCSC_ARCH,
libraryPath);
fullLibPath[sizeof(fullLibPath) - 1] = '\0';
GET_KEY(PCSCLITE_HP_MANUKEY_NAME, &manuIDs)
GET_KEY(PCSCLITE_HP_PRODKEY_NAME, &productIDs)
GET_KEY(PCSCLITE_HP_NAMEKEY_NAME, &readerNames)
if ((list_size(manuIDs) != list_size(productIDs))
|| (list_size(manuIDs) != list_size(readerNames)))
{
Log2(PCSC_LOG_CRITICAL, "Error parsing %s", fullPath);
(void)closedir(hpDir);
return -1;
}
/* Get CFBundleName */
rv = LTPBundleFindValueWithKey(&plist, PCSCLITE_HP_CFBUNDLE_NAME,
&values);
if (rv)
CFBundleName = NULL;
else
CFBundleName = strdup(list_get_at(values, 0));
/* while we find a nth ifdVendorID in Info.plist */
for (alias=0; alias<list_size(manuIDs); alias++)
{
char *value;
/* variables entries */
value = list_get_at(manuIDs, alias);
driverTracker[listCount].manuID = strtol(value, NULL, 16);
value = list_get_at(productIDs, alias);
driverTracker[listCount].productID = strtol(value, NULL, 16);
driverTracker[listCount].readerName = strdup(list_get_at(readerNames, alias));
/* constant entries for a same driver */
driverTracker[listCount].bundleName = strdup(currFP->d_name);
driverTracker[listCount].libraryPath = strdup(fullLibPath);
driverTracker[listCount].CFBundleName = CFBundleName;
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_INFO, "Found driver for: %s",
driverTracker[listCount].readerName);
#endif
listCount++;
if (listCount >= driverSize)
{
int i;
/* increase the array size */
driverSize += DRIVER_TRACKER_SIZE_STEP;
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_INFO,
"Increase driverTracker to %d entries", driverSize);
#endif
void* tmp = realloc(driverTracker,
driverSize * sizeof(*driverTracker));
if (NULL == tmp)
{
free(driverTracker);
Log1(PCSC_LOG_CRITICAL, "Not enough memory");
driverSize = -1;
(void)closedir(hpDir);
return -1;
}
driverTracker = tmp;
/* clean the newly allocated entries */
for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
{
driverTracker[i].manuID = 0;
driverTracker[i].productID = 0;
driverTracker[i].bundleName = NULL;
driverTracker[i].libraryPath = NULL;
driverTracker[i].readerName = NULL;
driverTracker[i].CFBundleName = NULL;
}
}
}
bundleRelease(&plist);
}
}
driverSize = listCount;
(void)closedir(hpDir);
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_INFO, "Found drivers for %d readers", listCount);
#endif
return 0;
} /* HPReadBundleValues */
/*@null@*/ static struct _driverTracker *get_driver(struct udev_device *dev,
const char *devpath, struct _driverTracker **classdriver)
{
int i;
unsigned int idVendor, idProduct;
static struct _driverTracker *driver;
const char *str;
str = udev_device_get_sysattr_value(dev, "idVendor");
if (!str)
{
Log1(PCSC_LOG_ERROR, "udev_device_get_sysattr_value() failed");
return NULL;
}
idVendor = strtol(str, NULL, 16);
str = udev_device_get_sysattr_value(dev, "idProduct");
if (!str)
{
Log1(PCSC_LOG_ERROR, "udev_device_get_sysattr_value() failed");
return NULL;
}
idProduct = strtol(str, NULL, 16);
#ifdef NO_LOG
(void)devpath;
#endif
Log4(PCSC_LOG_DEBUG,
"Looking for a driver for VID: 0x%04X, PID: 0x%04X, path: %s",
idVendor, idProduct, devpath);
*classdriver = NULL;
driver = NULL;
/* check if the device is supported by one driver */
for (i=0; i<driverSize; i++)
{
if (driverTracker[i].libraryPath != NULL &&
idVendor == driverTracker[i].manuID &&
idProduct == driverTracker[i].productID)
{
if ((driverTracker[i].CFBundleName != NULL)
&& (0 == strcmp(driverTracker[i].CFBundleName, "CCIDCLASSDRIVER")))
*classdriver = &driverTracker[i];
else
/* it is not a CCID Class driver */
driver = &driverTracker[i];
}
}
/* if we found a specific driver */
if (driver)
return driver;
/* else return the Class driver (if any) */
return *classdriver;
}
static void HPRemoveDevice(struct udev_device *dev)
{
int i;
const char *devpath;
struct udev_device *parent;
const char *sysname;
/* The device pointed to by dev contains information about
the interface. In order to get information about the USB
device, get the parent device with the subsystem/devtype pair
of "usb"/"usb_device". This will be several levels up the
tree, but the function will find it.*/
parent = udev_device_get_parent_with_subsystem_devtype(dev, "usb",
"usb_device");
if (!parent)
return;
devpath = udev_device_get_devnode(parent);
if (!devpath)
{
/* the device disapeared? */
Log1(PCSC_LOG_ERROR, "udev_device_get_devnode() failed");
return;
}
sysname = udev_device_get_sysname(dev);
if (!sysname)
{
Log1(PCSC_LOG_ERROR, "udev_device_get_sysname() failed");
return;
}
for (i=0; i<PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (readerTracker[i].fullName && !strcmp(sysname, readerTracker[i].sysname))
{
Log4(PCSC_LOG_INFO, "Removing USB device[%d]: %s at %s", i,
readerTracker[i].fullName, readerTracker[i].devpath);
RFRemoveReader(readerTracker[i].fullName, PCSCLITE_HP_BASE_PORT + i);
free(readerTracker[i].devpath);
readerTracker[i].devpath = NULL;
free(readerTracker[i].fullName);
readerTracker[i].fullName = NULL;
free(readerTracker[i].sysname);
readerTracker[i].sysname = NULL;
break;
}
}
}
static void HPAddDevice(struct udev_device *dev)
{
int index, a;
char *deviceName = NULL;
char *fullname = NULL;
struct _driverTracker *driver, *classdriver;
const char *sSerialNumber = NULL, *sInterfaceName = NULL;
const char *sInterfaceNumber;
LONG ret;
int bInterfaceNumber;
const char *devpath;
struct udev_device *parent;
const char *sysname;
/* The device pointed to by dev contains information about
the interface. In order to get information about the USB
device, get the parent device with the subsystem/devtype pair
of "usb"/"usb_device". This will be several levels up the
tree, but the function will find it.*/
parent = udev_device_get_parent_with_subsystem_devtype(dev, "usb",
"usb_device");
if (!parent)
return;
devpath = udev_device_get_devnode(parent);
if (!devpath)
{
/* the device disapeared? */
Log1(PCSC_LOG_ERROR, "udev_device_get_devnode() failed");
return;
}
driver = get_driver(parent, devpath, &classdriver);
if (NULL == driver)
{
/* not a smart card reader */
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_DEBUG, "%s is not a supported smart card reader",
devpath);
#endif
return;
}
sysname = udev_device_get_sysname(dev);
if (!sysname)
{
Log1(PCSC_LOG_ERROR, "udev_device_get_sysname() failed");
return;
}
/* check for duplicated add */
for (index=0; index<PCSCLITE_MAX_READERS_CONTEXTS; index++)
{
if (readerTracker[index].fullName && !strcmp(sysname, readerTracker[index].sysname))
return;
}
Log2(PCSC_LOG_INFO, "Adding USB device: %s", driver->readerName);
sInterfaceNumber = udev_device_get_sysattr_value(dev, "bInterfaceNumber");
if (sInterfaceNumber)
bInterfaceNumber = atoi(sInterfaceNumber);
else
bInterfaceNumber = 0;
a = asprintf(&deviceName, "usb:%04x/%04x:libudev:%d:%s",
driver->manuID, driver->productID, bInterfaceNumber, devpath);
if (-1 == a)
{
Log1(PCSC_LOG_ERROR, "asprintf() failed");
return;
}
/* find a free entry */
for (index=0; index<PCSCLITE_MAX_READERS_CONTEXTS; index++)
{
if (NULL == readerTracker[index].fullName)
break;
}
if (PCSCLITE_MAX_READERS_CONTEXTS == index)
{
Log2(PCSC_LOG_ERROR,
"Not enough reader entries. Already found %d readers", index);
return;
}
if (Add_Interface_In_Name)
sInterfaceName = udev_device_get_sysattr_value(dev, "interface");
if (Add_Serial_In_Name)
sSerialNumber = udev_device_get_sysattr_value(parent, "serial");
/* name from the Info.plist file */
fullname = strdup(driver->readerName);
/* interface name from the device (if any) */
if (sInterfaceName)
{
char *result;
/* create a new name */
a = asprintf(&result, "%s [%s]", fullname, sInterfaceName);
if (-1 == a)
{
Log1(PCSC_LOG_ERROR, "asprintf() failed");
goto exit;
}
free(fullname);
fullname = result;
}
/* serial number from the device (if any) */
if (sSerialNumber)
{
/* only add the serial number if it is not already present in the
* interface name */
if (!sInterfaceName || NULL == strstr(sInterfaceName, sSerialNumber))
{
char *result;
/* create a new name */
a = asprintf(&result, "%s (%s)", fullname, sSerialNumber);
if (-1 == a)
{
Log1(PCSC_LOG_ERROR, "asprintf() failed");
goto exit;
}
free(fullname);
fullname = result;
}
}
readerTracker[index].fullName = strdup(fullname);
readerTracker[index].devpath = strdup(devpath);
readerTracker[index].sysname = strdup(sysname);
ret = RFAddReader(fullname, PCSCLITE_HP_BASE_PORT + index,
driver->libraryPath, deviceName);
if ((SCARD_S_SUCCESS != ret) && (SCARD_E_UNKNOWN_READER != ret))
{
Log2(PCSC_LOG_ERROR, "Failed adding USB device: %s",
driver->readerName);
if (classdriver && driver != classdriver)
{
/* the reader can also be used by the a class driver */
ret = RFAddReader(fullname, PCSCLITE_HP_BASE_PORT + index,
classdriver->libraryPath, deviceName);
if ((SCARD_S_SUCCESS != ret) && (SCARD_E_UNKNOWN_READER != ret))
{
Log2(PCSC_LOG_ERROR, "Failed adding USB device: %s",
driver->readerName);
(void)CheckForOpenCT();
}
}
else
{
(void)CheckForOpenCT();
}
}
if (SCARD_S_SUCCESS != ret)
{
/* adding the reader failed */
free(readerTracker[index].devpath);
readerTracker[index].devpath = NULL;
free(readerTracker[index].fullName);
readerTracker[index].fullName = NULL;
free(readerTracker[index].sysname);
readerTracker[index].sysname = NULL;
}
exit:
free(fullname);
free(deviceName);
} /* HPAddDevice */
static void HPScanUSB(struct udev *udev)
{
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
/* Create a list of the devices in the 'usb' subsystem. */
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "usb");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
/* For each item enumerated */
udev_list_entry_foreach(dev_list_entry, devices)
{
struct udev_device *dev;
const char *devpath;
/* Get the filename of the /sys entry for the device
and create a udev_device object (dev) representing it */
devpath = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, devpath);
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_DEBUG, "Found matching USB device: %s", devpath);
#endif
HPAddDevice(dev);
/* free device */
udev_device_unref(dev);
}
/* Free the enumerator object */
udev_enumerate_unref(enumerate);
}
static void * HPEstablishUSBNotifications(void *arg)
{
struct udev_monitor *udev_monitor = arg;
int r;
int fd;
struct pollfd pfd;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
/* udev monitor file descriptor */
fd = udev_monitor_get_fd(udev_monitor);
if (fd < 0)
{
Log2(PCSC_LOG_ERROR, "udev_monitor_get_fd() error: %d", fd);
pthread_exit(NULL);
}
pfd.fd = fd;
pfd.events = POLLIN;
for (;;)
{
struct udev_device *dev;
#ifdef DEBUG_HOTPLUG
Log0(PCSC_LOG_INFO);
#endif
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
/* wait for a udev event */
r = TEMP_FAILURE_RETRY(poll(&pfd, 1, -1));
if (r < 0)
{
Log2(PCSC_LOG_ERROR, "select(): %s", strerror(errno));
pthread_exit(NULL);
}
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
dev = udev_monitor_receive_device(udev_monitor);
if (dev)
{
const char *action = udev_device_get_action(dev);
if (action)
{
if (!strcmp("remove", action))
{
Log1(PCSC_LOG_INFO, "USB Device removed");
HPRemoveDevice(dev);
}
else
if (!strcmp("add", action))
{
Log1(PCSC_LOG_INFO, "USB Device add");
HPAddDevice(dev);
}
}
/* free device */
udev_device_unref(dev);
}
}
pthread_exit(NULL);
} /* HPEstablishUSBNotifications */
/***
* Start a thread waiting for hotplug events
*/
LONG HPSearchHotPluggables(void)
{
int i;
for (i=0; i<PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
readerTracker[i].devpath = NULL;
readerTracker[i].fullName = NULL;
readerTracker[i].sysname = NULL;
}
return HPReadBundleValues();
} /* HPSearchHotPluggables */
/**
* Stop the hotplug thread
*/
LONG HPStopHotPluggables(void)
{
int i;
if (driverSize <= 0)
return 0;
if (!Udev)
return 0;
pthread_cancel(usbNotifyThread);
pthread_join(usbNotifyThread, NULL);
for (i=0; i<driverSize; i++)
{
/* free strings allocated by strdup() */
free(driverTracker[i].bundleName);
free(driverTracker[i].libraryPath);
free(driverTracker[i].readerName);
}
free(driverTracker);
udev_unref(Udev);
Udev = NULL;
driverSize = -1;
Log1(PCSC_LOG_INFO, "Hotplug stopped");
return 0;
} /* HPStopHotPluggables */
/**
* Sets up callbacks for device hotplug events.
*/
ULONG HPRegisterForHotplugEvents(void)
{
struct udev_monitor *udev_monitor;
int r;
if (driverSize <= 0)
{
Log1(PCSC_LOG_INFO, "No bundle files in pcsc drivers directory: "
PCSCLITE_HP_DROPDIR);
Log1(PCSC_LOG_INFO, "Disabling USB support for pcscd");
return 0;
}
/* Create the udev object */
Udev = udev_new();
if (!Udev)
{
Log1(PCSC_LOG_ERROR, "udev_new() failed");
return SCARD_F_INTERNAL_ERROR;
}
udev_monitor = udev_monitor_new_from_netlink(Udev, "udev");
if (NULL == udev_monitor)
{
Log1(PCSC_LOG_ERROR, "udev_monitor_new_from_netlink() error");
pthread_exit(NULL);
}
/* filter only the interfaces */
r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb",
"usb_interface");
if (r)
{
Log2(PCSC_LOG_ERROR, "udev_monitor_filter_add_match_subsystem_devtype() error: %d\n", r);
pthread_exit(NULL);
}
r = udev_monitor_enable_receiving(udev_monitor);
if (r)
{
Log2(PCSC_LOG_ERROR, "udev_monitor_enable_receiving() error: %d\n", r);
pthread_exit(NULL);
}
/* scan the USB bus at least once before accepting client connections */
HPScanUSB(Udev);
if (ThreadCreate(&usbNotifyThread, 0,
(PCSCLITE_THREAD_FUNCTION( )) HPEstablishUSBNotifications, udev_monitor))
{
Log1(PCSC_LOG_ERROR, "ThreadCreate() failed");
return SCARD_F_INTERNAL_ERROR;
}
return 0;
} /* HPRegisterForHotplugEvents */
void HPReCheckSerialReaders(void)
{
/* nothing to do here */
#ifdef DEBUG_HOTPLUG
Log0(PCSC_LOG_ERROR);
#endif
} /* HPReCheckSerialReaders */
#endif