pcsc-lite  2.5.0
dyn_unix.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
3  *
4  * Copyright (C) 1999-2002
5  * David Corcoran <corcoran@musclecard.com>
6  * Copyright (C) 2002-2024
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 
38 #include "config.h"
39 #include <stdio.h>
40 #include <string.h>
41 #ifndef __APPLE__
42 #include <dlfcn.h>
43 #include <stdlib.h>
44 #include <stdbool.h>
45 
46 #include "misc.h"
47 #include "pcsclite.h"
48 #include "debuglog.h"
49 #include "dyn_generic.h"
50 
51 INTERNAL void * DYN_LoadLibrary(const char *pcLibrary)
52 {
53  void *pvLHandle = NULL;
54 #ifndef PCSCLITE_STATIC_DRIVER
55  pvLHandle = dlopen(pcLibrary, RTLD_LAZY);
56 
57  if (pvLHandle == NULL)
58  {
59  Log3(PCSC_LOG_CRITICAL, "%s: %s", pcLibrary, dlerror());
60  }
61 #else
62  (void)pcLibrary;
63 #endif
64 
65  return pvLHandle;
66 }
67 
68 INTERNAL LONG DYN_CloseLibrary(void *pvLHandle)
69 {
70 #ifndef PCSCLITE_STATIC_DRIVER
71  int ret;
72 
73  ret = dlclose(pvLHandle);
74 
75  if (ret)
76  {
77  Log2(PCSC_LOG_CRITICAL, "%s", dlerror());
78  return SCARD_F_UNKNOWN_ERROR;
79  }
80 #else
81  (void)pvLHandle;
82 #endif
83 
84  return SCARD_S_SUCCESS;
85 }
86 
87 INTERNAL LONG DYN_GetAddress(void *pvLHandle, void **pvFHandle,
88  const char *pcFunction, bool mayfail)
89 {
90  char pcFunctionName[256];
91  LONG rv = SCARD_S_SUCCESS;
92 
93  /* Some platforms might need a leading underscore for the symbol */
94  (void)snprintf(pcFunctionName, sizeof(pcFunctionName), "_%s", pcFunction);
95 
96  *pvFHandle = NULL;
97 #ifndef PCSCLITE_STATIC_DRIVER
98  *pvFHandle = dlsym(pvLHandle, pcFunctionName);
99 
100  /* Failed? Try again without the leading underscore */
101  if (*pvFHandle == NULL)
102  *pvFHandle = dlsym(pvLHandle, pcFunction);
103 
104  if (*pvFHandle == NULL)
105  {
106 #ifdef NO_LOG
107  (void)mayfail;
108 #endif
109  Log3(mayfail ? PCSC_LOG_INFO : PCSC_LOG_CRITICAL, "%s: %s",
110  pcFunction, dlerror());
112  }
113 #else
114  (void)pvLHandle;
115  (void)pvFHandle;
116  (void)mayfail;
117 #endif
118 
119  return rv;
120 }
121 
122 #endif /* !__APPLE__ */
This abstracts dynamic library loading functions.
#define SCARD_S_SUCCESS
No error was encountered.
Definition: pcsclite.h:107
#define SCARD_F_UNKNOWN_ERROR
An internal error has been detected, but the source is unknown.
Definition: pcsclite.h:147
This keeps a list of defines for pcsc-lite.
This handles debugging.